# Modifier.Node

Artifact: [androidx.compose.ui:ui](https://developer.android.com/jetpack/androidx/releases/compose-ui) [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/Modifier.kt+class:androidx.compose.ui.Modifier.Node) Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.3.0) Common/AllAndroid/JVM
- Cmn

  ```
  abstract class Modifier.Node : DelegatableNode
  ```
Known direct subclasses [DelegatingNode](https://developer.android.com/reference/kotlin/androidx/compose/ui/node/DelegatingNode)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/node/DelegatingNode` | A `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node` which is able to delegate work to other `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node` instances. |

*** ** * ** ***

The longer-lived object that is created for each `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Element` applied to a `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/Layout.composable#Layout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.layout.MeasurePolicy)`. Most `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node` implementations will have a corresponding "Modifier Factory" extension method on Modifier that will allow them to be used indirectly, without ever implementing a `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node` subclass directly. In some cases it may be useful to define a custom `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node` subclass in order to efficiently implement some collection of behaviors that requires maintaining state over time and over many recompositions where the various provided Modifier factories are not sufficient.

When a `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier` is set on a `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/Layout.composable#Layout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.layout.MeasurePolicy)`, each `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Element` contained in that linked list will result in a corresponding `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node` instance in a matching linked list of `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node`s that the `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/Layout.composable#Layout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.layout.MeasurePolicy)` will hold on to. As subsequent `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier` chains get set on the `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/Layout.composable#Layout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.layout.MeasurePolicy)`, the linked list of `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node`s will be diffed and updated as appropriate, even though the `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier` instance might be completely new. As a result, the lifetime of a `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node` is the intersection of the lifetime of the `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/Layout.composable#Layout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.layout.MeasurePolicy)` that it lives on and a corresponding `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Element` being present in the `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/Layout.composable#Layout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.layout.MeasurePolicy)`'s `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier`.

If one creates a subclass of `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node`, it is expected that it will implement one or more interfaces that interact with the various Compose UI subsystems. To use the `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node` subclass, it is expected that it will be instantiated by adding a `https://developer.android.com/reference/kotlin/androidx/compose/ui/node/ModifierNodeElement` to a `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier` chain.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/node/ModifierNodeElement` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/node/CompositionLocalConsumerModifierNode` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/node/DelegatableNode` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/node/DelegatingNode` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/node/LayoutModifierNode` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/node/DrawModifierNode` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/node/SemanticsModifierNode` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/node/PointerInputModifierNode` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/modifier/ModifierLocalModifierNode` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/node/ParentDataModifierNode` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/node/LayoutAwareModifierNode` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/node/GlobalPositionAwareModifierNode` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/ApproachLayoutModifierNode` |   |

## Summary

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node#Node()()` | Cmn |

| ### Public functions |
|---|---|---|
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/EmptySuper https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node#onAttach()()` Called when the node is attached to a `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/Layout.composable#Layout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.layout.MeasurePolicy)` which is part of the UI tree. | Cmn |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/EmptySuper https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node#onDetach()()` Called when the node is not attached to a `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/Layout.composable#Layout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.layout.MeasurePolicy)` which is not a part of the UI tree anymore. | Cmn |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/EmptySuper https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node#onReset()()` Called when the node is about to be moved to a pool of layouts ready to be reused. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node#sideEffect(kotlin.Function0)(effect: () -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)` This can be called to register `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node#sideEffect(kotlin.Function0)` as a function to be executed after all of the changes to the tree are applied. | Cmn |

| ### Public properties |
|---|---|---|
| `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node#coroutineScope()` A `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html` that can be used to launch tasks that should run while the node is attached. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node#isAttached()` Indicates that the node is attached to a `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/Layout.composable#Layout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.layout.MeasurePolicy)` which is part of the UI tree. | Cmn |
| `final https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node#node()` A reference of the `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node` that holds this node's position in the node hierarchy. | Cmn |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node#shouldAutoInvalidate()` If this property returns `true`, then nodes will be automatically invalidated after the modifier update completes (For example, if the returned Node is a `https://developer.android.com/reference/kotlin/androidx/compose/ui/node/DrawModifierNode`, its `https://developer.android.com/reference/kotlin/androidx/compose/ui/node/package-summary#(androidx.compose.ui.node.DrawModifierNode).invalidateDraw()` function will be invoked automatically as part of auto invalidation). | Cmn |

| ### Extension functions |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `<T : https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node & https://developer.android.com/reference/kotlin/androidx/compose/ui/node/ObserverModifierNode> T.https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node#(androidx.compose.ui.Modifier.Node).observeReads(kotlin.Function0)(block: () -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)` Use this function to observe snapshot reads for any target within the specified `https://developer.android.com/reference/kotlin/androidx/compose/ui/node/package-summary#(androidx.compose.ui.Modifier.Node).observeReads(kotlin.Function0)`. | Cmn |

### Inherited functions

From [androidx.compose.ui.node.DelegatableNode](https://developer.android.com/reference/kotlin/androidx/compose/ui/node/DelegatableNode) `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `@https://developer.android.com/reference/kotlin/androidx/annotation/EmptySuper
https://developer.android.com/reference/kotlin/androidx/compose/ui/node/DelegatableNode#onDensityChange()()`

Invoked when the density changes for this node.
Cmn `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `@https://developer.android.com/reference/kotlin/androidx/annotation/EmptySuper
https://developer.android.com/reference/kotlin/androidx/compose/ui/node/DelegatableNode#onLayoutDirectionChange()()`

Invoked when the layout direction changes for this node.
Cmn

## Public constructors

### Node

Cmn Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.3.0)

```
Node()
```

## Public functions

### onAttach

Cmn Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.3.0)

```
@EmptySuper
open fun onAttach(): Unit
```

Called when the node is attached to a `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/Layout.composable#Layout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.layout.MeasurePolicy)` which is part of the UI tree. When called, `node` is guaranteed to be non-null. You can call sideEffect, coroutineScope, etc. This is not guaranteed to get called at a time where the rest of the Modifier.Nodes in the hierarchy are "up to date". For instance, at the time of calling onAttach for this node, another node may be in the tree that will be detached by the time Compose has finished applying changes. As a result, if you need to guarantee that the state of the tree is "final" for this round of changes, you should use the `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node#sideEffect(kotlin.Function0)` API to schedule the calculation to be done at that time.

### onDetach

Cmn Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.3.0)

```
@EmptySuper
open fun onDetach(): Unit
```

Called when the node is not attached to a `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/Layout.composable#Layout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.layout.MeasurePolicy)` which is not a part of the UI tree anymore. Note that the node can be reattached again.

This should be called right before the node gets removed from the list, so you should still be able to traverse inside of this method. Ideally we would not allow you to trigger side effects here.

### onReset

Cmn Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.4.0)

```
@EmptySuper
open fun onReset(): Unit
```

Called when the node is about to be moved to a pool of layouts ready to be reused. For example it happens when the node is part of the item of LazyColumn after this item is scrolled out of the viewport. This means this node could be in future reused for a `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/Layout.composable#Layout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.layout.MeasurePolicy)` displaying a semantically different content when the list will be populating a new item.

Use this callback to reset some local item specific state, like "is my component focused".

This callback is called while the node is attached. Right after this callback the node will be detached and later reattached when reused.

```kotlin
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier

class SelectableNode : Modifier.Node() {
    var selected by mutableStateOf(false)

    override fun onReset() {
        // reset `selected` to the initial value as if the node will be reused for
        // displaying different content it shouldn't be selected straight away.
        selected = false
    }

    // some logic which sets `selected` to true when it is selected
}
```

### sideEffect

Cmn Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.3.0)

```
fun sideEffect(effect: () -> Unit): Unit
```

This can be called to register `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node#sideEffect(kotlin.Function0)` as a function to be executed after all of the changes to the tree are applied.

This API can only be called if the node `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node#isAttached()`.

## Public properties

### coroutineScope

Cmn Added in [1.5.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.5.0)

```
val coroutineScope: CoroutineScope
```

A `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html` that can be used to launch tasks that should run while the node is attached.

The scope is accessible between `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node#onAttach()` and `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node#onDetach()` calls, and will be cancelled after the node is detached (after `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node#onDetach()` returns).

```kotlin
import androidx.compose.animation.core.Animatable
import androidx.compose.ui.Modifier

class AnimatedNode : Modifier.Node() {
    val animatable = Animatable(0f)

    override fun onAttach() {
        coroutineScope.launch { animatable.animateTo(1f) }
    }
}
```

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` | If called while the node is not attached. |

### isAttached

Cmn Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.3.0)

```
val isAttached: Boolean
```

Indicates that the node is attached to a `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/Layout.composable#Layout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.layout.MeasurePolicy)` which is part of the UI tree. This will get set to true right before `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node#onAttach()` is called, and set to false right after `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node#onDetach()` is called.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node#onAttach()` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node#onDetach()` |   |

### node

Cmn

```
final val node: Modifier.Node
```

A reference of the `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.Node` that holds this node's position in the node hierarchy. If the node is a delegate of another node, this will point to the root delegating node that is actually part of the node tree. Otherwise, this will point to itself.

### shouldAutoInvalidate

Cmn Added in [1.5.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.5.0)

```
open val shouldAutoInvalidate: Boolean
```

If this property returns `true`, then nodes will be automatically invalidated after the modifier update completes (For example, if the returned Node is a `https://developer.android.com/reference/kotlin/androidx/compose/ui/node/DrawModifierNode`, its `https://developer.android.com/reference/kotlin/androidx/compose/ui/node/package-summary#(androidx.compose.ui.node.DrawModifierNode).invalidateDraw()` function will be invoked automatically as part of auto invalidation).

This is enabled by default, and provides a convenient mechanism to schedule invalidation and apply changes made to the modifier. You may choose to set this to `false` if your modifier has auto-invalidatable properties that do not frequently require invalidation to improve performance by skipping unnecessary invalidation. If `autoInvalidate` is set to `false`, you must call the appropriate invalidate functions manually when the modifier is updated or else the updates may not be reflected in the UI appropriately.

## Extension functions

### T.observeReads

Cmn Artifact: [androidx.compose.ui:ui](https://developer.android.com/jetpack/androidx/releases/compose-ui) [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/node/ObserverModifierNode.kt+function:observeReads)

```
fun <T : Modifier.Node & ObserverModifierNode> T.observeReads(block: () -> Unit): Unit
```

Use this function to observe snapshot reads for any target within the specified `https://developer.android.com/reference/kotlin/androidx/compose/ui/node/package-summary#(androidx.compose.ui.Modifier.Node).observeReads(kotlin.Function0)`. `https://developer.android.com/reference/kotlin/androidx/compose/ui/node/ObserverModifierNode#onObservedReadsChanged()` is called when any of the observed values within the snapshot change.