# rememberUpdatedState

Common/AllAndroid/JVMNative/C/iOSJavaScript

## Functions summary

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State<T>` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable <T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/compose/runtime/rememberUpdatedState.composable#rememberUpdatedState(kotlin.Any)(newValue: T)` `https://developer.android.com/reference/kotlin/androidx/compose/runtime/remember.composable#remember(kotlin.Function0)` a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#rememberUpdatedState(kotlin.Any)` and update its value to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#rememberUpdatedState(kotlin.Any)` on each recomposition of the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/rememberUpdatedState.composable#rememberUpdatedState(kotlin.Any)` call. | Cmn |

## Functions

### rememberUpdatedState

Cmn Artifact: [androidx.compose.runtime:runtime](https://developer.android.com/jetpack/androidx/releases/compose-runtime) [View Source](https://cs.android.com/search?q=file:androidx/compose/runtime/SnapshotState.kt+function:rememberUpdatedState) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
@Composable
fun <T : Any?> rememberUpdatedState(newValue: T): State<T>
```

`https://developer.android.com/reference/kotlin/androidx/compose/runtime/remember.composable#remember(kotlin.Function0)` a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#rememberUpdatedState(kotlin.Any)` and update its value to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#rememberUpdatedState(kotlin.Any)` on each recomposition of the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/rememberUpdatedState.composable#rememberUpdatedState(kotlin.Any)` call.

`https://developer.android.com/reference/kotlin/androidx/compose/runtime/rememberUpdatedState.composable#rememberUpdatedState(kotlin.Any)` should be used when parameters or values computed during composition are referenced by a long-lived lambda or object expression. Recomposition will update the resulting `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State` without recreating the long-lived lambda or object, allowing that object to persist without cancelling and resubscribing, or relaunching a long-lived operation that may be expensive or prohibitive to recreate and restart. This may be common when working with `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Function1)` or `https://developer.android.com/reference/kotlin/androidx/compose/runtime/LaunchedEffect.composable#LaunchedEffect(kotlin.coroutines.SuspendFunction1)`, for example:

```kotlin
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState

@Composable
fun EventHandler(dispatcher: Dispatcher, onEvent: () -> Unit) {
    val currentOnEvent by rememberUpdatedState(onEvent)

    // Event handlers are ordered and a new onEvent should not cause us to re-register,
    // losing our position in the dispatcher.
    DisposableEffect(dispatcher) {
        val disposable = dispatcher.addListener {
            // currentOnEvent will always refer to the latest onEvent function that
            // the EventHandler was recomposed with
            currentOnEvent()
        }
        onDispose { disposable.dispose() }
    }
}
```

`https://developer.android.com/reference/kotlin/androidx/compose/runtime/LaunchedEffect.composable#LaunchedEffect(kotlin.coroutines.SuspendFunction1)`s often describe state machines that should not be reset and restarted if a parameter or event callback changes, but they should have the current value available when needed. For example:

```kotlin
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState

@Composable
fun NotificationHost(state: NotificationState, onTimeout: (Notification) -> Unit) {
    val currentOnTimeout by rememberUpdatedState(onTimeout)

    state.currentNotification?.let { currentNotification ->
        LaunchedEffect(currentNotification) {
            // We should not restart this delay if onTimeout changes, but we want to call
            // the onTimeout we were last recomposed with when it completes.
            delay(NotificationTimeout)
            currentOnTimeout(currentNotification)
        }
    }

    // ...
}
```

By using `https://developer.android.com/reference/kotlin/androidx/compose/runtime/rememberUpdatedState.composable#rememberUpdatedState(kotlin.Any)` a composable function can update these operations in progress.