---
title: "modifierLocalOf"
description: "Creates a [ProvidableModifierLocal] and specifies a default factory.


Here are examples where a modifier can communicate with another in the same modifier chain:

Sample 1: Modifier sending a message to another to its right.


Sample 2: Modifier sending a message to another to its left.


Here are examples where a modifier can communicate with another across layout nodes:

Sample 1: Modifier sending a message to a modifier on a child layout node.


Sample 2: Modifier sending a message to a modifier on a parent layout node."
type: "function"
---

<div class='type'>Function</div>


<a id='references'></a>
<div class='sourceset sourceset-common'>Common</div>


```kotlin
fun <T> modifierLocalOf(defaultFactory: () -> T): ProvidableModifierLocal<T>
```


Creates a `ProvidableModifierLocal` and specifies a default factory.


Here are examples where a modifier can communicate with another in the same modifier chain:

Sample 1: Modifier sending a message to another to its right.


Sample 2: Modifier sending a message to another to its left.


Here are examples where a modifier can communicate with another across layout nodes:

Sample 1: Modifier sending a message to a modifier on a child layout node.


Sample 2: Modifier sending a message to a modifier on a parent layout node.

#### Parameters

| | |
| --- | --- |
| defaultFactory | a factory to create a default value in cases where a `ModifierLocal` is consumed without a Provider. If this is a situation you would rather not handle, you can throw an error in this factory. |




## Code Examples
### ModifierLocalChildParentCommunicationInterLayoutNodeSample
```kotlin
@Composable
fun ModifierLocalChildParentCommunicationInterLayoutNodeSample() {
    class Sender(val onMessageReceived: (String) -> Unit) {
        fun sendMessage(message: String) {
            onMessageReceived(message)
        }
    }
    // Define the type of data.
    val ModifierLocalSender = modifierLocalOf<Sender> { error("No sender provided by parent.") }
    Box(
        Modifier
            // Provide an instance associated with the sender type.
            .modifierLocalProvider(ModifierLocalSender) {
                Sender { println("Message Received: $it") }
            }
    ) {
        var sender by remember { mutableStateOf<Sender?>(null) }
        Box(
            Modifier
                // Use the sender type to fetch an instance.
                .modifierLocalConsumer { sender = ModifierLocalSender.current }
                // Use this instance to send a message to the parent.
                .clickable { sender?.sendMessage("Hello World") }
        )
    }
}
```
### ModifierLocalChildParentCommunicationWithinLayoutNodeSample
```kotlin
@Composable
fun ModifierLocalChildParentCommunicationWithinLayoutNodeSample() {
    class Sender(val onMessageReceived: (String) -> Unit) {
        fun sendMessage(message: String) {
            onMessageReceived(message)
        }
    }
    // Define the type of data.
    val ModifierLocalSender = modifierLocalOf<Sender> { error("No sender provided by parent.") }
    Box(
        Modifier
            // Provide an instance associated with the sender type.
            .modifierLocalProvider(ModifierLocalSender) {
                Sender { println("Message Received: $it") }
            }
            .composed {
                var sender by remember { mutableStateOf<Sender?>(null) }
                Modifier
                    // Use the sender type to fetch an instance.
                    .modifierLocalConsumer { sender = ModifierLocalSender.current }
                    // Use this instance to send a message to the parent.
                    .clickable { sender?.sendMessage("Hello World") }
            }
    )
}
```
### ModifierLocalParentChildCommunicationInterLayoutNodeSample
```kotlin
@Composable
fun ModifierLocalParentChildCommunicationInterLayoutNodeSample() {
    // Define the type of data.
    val ModifierLocalMessage = modifierLocalOf { "Unknown" }
    Box(
        // Provide an instance associated with the data type.
        Modifier.modifierLocalProvider(ModifierLocalMessage) { "World" }
    ) {
        var message by remember { mutableStateOf("") }
        Box(
            Modifier
                // Use the data type to read the message.
                .modifierLocalConsumer { message = ModifierLocalMessage.current }
                .clickable { println("Hello $message") }
        )
    }
}
```
### ModifierLocalParentChildCommunicationWithinLayoutNodeSample
```kotlin
@Composable
fun ModifierLocalParentChildCommunicationWithinLayoutNodeSample() {
    // Define the type of data.
    val ModifierLocalMessage = modifierLocalOf { "Unknown" }
    Box(
        Modifier
            // Provide an instance associated with the data type.
            .modifierLocalProvider(ModifierLocalMessage) { "World" }
            .composed {
                var message by remember { mutableStateOf("") }
                Modifier
                    // Use the data type to read the message.
                    .modifierLocalConsumer { message = ModifierLocalMessage.current }
                    .clickable { println("Hello $message") }
            }
    )
}
```

