# DisposableEffect

Common/AllAndroid/JVMNative/C/iOSJavaScript

## Functions summary

|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable @https://developer.android.com/reference/kotlin/androidx/compose/runtime/NonRestartableComposable [DisposableEffect](https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Function1))(effect: https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope.() -> https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectResult)` **This function is deprecated.** DisposableEffect must provide one or more 'key' parameters that define the identity of the DisposableEffect and determine when its previous effect should be disposed and a new effect started for the new key. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable @https://developer.android.com/reference/kotlin/androidx/compose/runtime/NonRestartableComposable https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Function1)(key1: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, effect: https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope.() -> https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectResult)` A side effect of composition that must run for any new unique value of `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Function1)` and must be reversed or cleaned up if `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Function1)` changes or if the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Function1)` leaves the composition. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable @https://developer.android.com/reference/kotlin/androidx/compose/runtime/NonRestartableComposable https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Array,kotlin.Function1)(vararg keys: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, effect: https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope.() -> https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectResult)` A side effect of composition that must run for any new unique value of `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Array,kotlin.Function1)` and must be reversed or cleaned up if any `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Array,kotlin.Function1)` change or if the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Array,kotlin.Function1)` leaves the composition. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable @https://developer.android.com/reference/kotlin/androidx/compose/runtime/NonRestartableComposable https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Function1)(key1: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, key2: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, effect: https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope.() -> https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectResult)` A side effect of composition that must run for any new unique value of `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Function1)` or `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Function1)` and must be reversed or cleaned up if `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Function1)` or `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Function1)` changes, or if the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Function1)` leaves the composition. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable @https://developer.android.com/reference/kotlin/androidx/compose/runtime/NonRestartableComposable https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)( key1: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, key2: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, key3: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, effect: https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope.() -> https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectResult )` A side effect of composition that must run for any new unique value of `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)`, `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` or `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` and must be reversed or cleaned up if `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)`, `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` or `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` changes, or if the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` leaves the composition. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable @https://developer.android.com/reference/kotlin/androidx/compose/runtime/NonRestartableComposable https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)( key1: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, key2: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, key3: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, key4: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, effect: https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope.() -> https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectResult )` A side effect of composition that must run for any new unique value of `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)`, `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)`, `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` or `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` and must be reversed or cleaned up if `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)`, `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)`, `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` or `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` changes, or if the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` leaves the composition. | Cmn |

## Functions

### DisposableEffect

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/Effects.kt+function:DisposableEffect)

```
@Composable
@NonRestartableComposable
fun [DisposableEffect](https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Function1))(effect: DisposableEffectScope.() -> DisposableEffectResult): Unit
```

> [!CAUTION]
> **This function is deprecated.**   
> DisposableEffect must provide one or more 'key' parameters that define the identity of the DisposableEffect and determine when its previous effect should be disposed and a new effect started for the new key.

A side effect of composition that must be reversed or cleaned up if the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Function1)` leaves the composition.

It is an error to call `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Function1)` without at least one `key` parameter.

### DisposableEffect

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/Effects.kt+function:DisposableEffect)

```
@Composable
@NonRestartableComposable
fun DisposableEffect(key1: Any?, effect: DisposableEffectScope.() -> DisposableEffectResult): Unit
```

A side effect of composition that must run for any new unique value of `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Function1)` and must be reversed or cleaned up if `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Function1)` changes or if the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Function1)` leaves the composition.

A `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Function1)`'s *key* is a value that defines the identity of the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Function1)`. If a key changes, the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Function1)` must `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope#onDispose(kotlin.Function0)` its current `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Function1)` and reset by calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Function1)` again. Examples of keys include:

- Observable objects that the effect subscribes to

- Unique request parameters to an operation that must cancel and retry if those parameters change

`https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Function1)` may be used to initialize or subscribe to a key and reinitialize when a different key is provided, performing cleanup for the old operation before initializing the new. For example:

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

@Composable
fun UserProfile(userRepository: UserRepository, userRequest: UserDataRequest) {
    var userDataState by remember { mutableStateOf<UserDataState>(UserDataState.Loading) }

    // If either the repository or request change, we must cancel our old data fetch
    // and begin a new data fetch. We will cancel the current data fetch if UserProfile
    // leaves the composition.
    DisposableEffect(userRepository, userRequest) {
        val requestDisposable =
            userRepository.fetchUserData(
                userRequest,
                onSuccess = { response -> userDataState = UserDataState.UserData(response) },
                onError = { throwable ->
                    userDataState = UserDataState.Error(throwable.message)
                },
            )

        onDispose { requestDisposable.dispose() }
    }

    // ...
}
```

A `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Function1)` **must** include an `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope#onDispose(kotlin.Function0)` clause as the final statement in its `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Function1)` block. If your operation does not require disposal it might be a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/SideEffect.composable#SideEffect(kotlin.Function0)` instead, or a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/LaunchedEffect.composable#LaunchedEffect(kotlin.coroutines.SuspendFunction1)` if it launches a coroutine that should be managed by the composition.

There is guaranteed to be one call to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope#onDispose(kotlin.Function0)` for every call to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Function1)`. Both `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Function1)` and `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope#onDispose(kotlin.Function0)` will always be run on the composition's apply dispatcher and appliers are never run concurrent with themselves, one another, applying changes to the composition tree, or running `https://developer.android.com/reference/kotlin/androidx/compose/runtime/RememberObserver` event callbacks.

### DisposableEffect

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/Effects.kt+function:DisposableEffect)

```
@Composable
@NonRestartableComposable
fun DisposableEffect(vararg keys: Any?, effect: DisposableEffectScope.() -> DisposableEffectResult): Unit
```

A side effect of composition that must run for any new unique value of `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Array,kotlin.Function1)` and must be reversed or cleaned up if any `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Array,kotlin.Function1)` change or if the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Array,kotlin.Function1)` leaves the composition.

A `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Array,kotlin.Function1)`'s *key* is a value that defines the identity of the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Array,kotlin.Function1)`. If a key changes, the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Array,kotlin.Function1)` must `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope#onDispose(kotlin.Function0)` its current `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Array,kotlin.Function1)` and reset by calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Array,kotlin.Function1)` again. Examples of keys include:

- Observable objects that the effect subscribes to

- Unique request parameters to an operation that must cancel and retry if those parameters change

`https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Array,kotlin.Function1)` may be used to initialize or subscribe to a key and reinitialize when a different key is provided, performing cleanup for the old operation before initializing the new. For example:

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

@Composable
fun UserProfile(userRepository: UserRepository, userRequest: UserDataRequest) {
    var userDataState by remember { mutableStateOf<UserDataState>(UserDataState.Loading) }

    // If either the repository or request change, we must cancel our old data fetch
    // and begin a new data fetch. We will cancel the current data fetch if UserProfile
    // leaves the composition.
    DisposableEffect(userRepository, userRequest) {
        val requestDisposable =
            userRepository.fetchUserData(
                userRequest,
                onSuccess = { response -> userDataState = UserDataState.UserData(response) },
                onError = { throwable ->
                    userDataState = UserDataState.Error(throwable.message)
                },
            )

        onDispose { requestDisposable.dispose() }
    }

    // ...
}
```

A `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Array,kotlin.Function1)` **must** include an `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope#onDispose(kotlin.Function0)` clause as the final statement in its `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Array,kotlin.Function1)` block. If your operation does not require disposal it might be a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/SideEffect.composable#SideEffect(kotlin.Function0)` instead, or a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/LaunchedEffect.composable#LaunchedEffect(kotlin.coroutines.SuspendFunction1)` if it launches a coroutine that should be managed by the composition.

There is guaranteed to be one call to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope#onDispose(kotlin.Function0)` for every call to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Array,kotlin.Function1)`. Both `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Array,kotlin.Function1)` and `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope#onDispose(kotlin.Function0)` will always be run on the composition's apply dispatcher and appliers are never run concurrent with themselves, one another, applying changes to the composition tree, or running `https://developer.android.com/reference/kotlin/androidx/compose/runtime/RememberObserver` event callbacks.

### DisposableEffect

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/Effects.kt+function:DisposableEffect)

```
@Composable
@NonRestartableComposable
fun DisposableEffect(key1: Any?, key2: Any?, effect: DisposableEffectScope.() -> DisposableEffectResult): Unit
```

A side effect of composition that must run for any new unique value of `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Function1)` or `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Function1)` and must be reversed or cleaned up if `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Function1)` or `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Function1)` changes, or if the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Function1)` leaves the composition.

A `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Function1)`'s *key* is a value that defines the identity of the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Function1)`. If a key changes, the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Function1)` must `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope#onDispose(kotlin.Function0)` its current `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Function1)` and reset by calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Function1)` again. Examples of keys include:

- Observable objects that the effect subscribes to

- Unique request parameters to an operation that must cancel and retry if those parameters change

`https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Function1)` may be used to initialize or subscribe to a key and reinitialize when a different key is provided, performing cleanup for the old operation before initializing the new. For example:

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

@Composable
fun UserProfile(userRepository: UserRepository, userRequest: UserDataRequest) {
    var userDataState by remember { mutableStateOf<UserDataState>(UserDataState.Loading) }

    // If either the repository or request change, we must cancel our old data fetch
    // and begin a new data fetch. We will cancel the current data fetch if UserProfile
    // leaves the composition.
    DisposableEffect(userRepository, userRequest) {
        val requestDisposable =
            userRepository.fetchUserData(
                userRequest,
                onSuccess = { response -> userDataState = UserDataState.UserData(response) },
                onError = { throwable ->
                    userDataState = UserDataState.Error(throwable.message)
                },
            )

        onDispose { requestDisposable.dispose() }
    }

    // ...
}
```

A `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Function1)` **must** include an `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope#onDispose(kotlin.Function0)` clause as the final statement in its `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Function1)` block. If your operation does not require disposal it might be a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/SideEffect.composable#SideEffect(kotlin.Function0)` instead, or a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/LaunchedEffect.composable#LaunchedEffect(kotlin.coroutines.SuspendFunction1)` if it launches a coroutine that should be managed by the composition.

There is guaranteed to be one call to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope#onDispose(kotlin.Function0)` for every call to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Function1)`. Both `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Function1)` and `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope#onDispose(kotlin.Function0)` will always be run on the composition's apply dispatcher and appliers are never run concurrent with themselves, one another, applying changes to the composition tree, or running `https://developer.android.com/reference/kotlin/androidx/compose/runtime/RememberObserver` event callbacks.

### DisposableEffect

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/Effects.kt+function:DisposableEffect)

```
@Composable
@NonRestartableComposable
fun DisposableEffect(
    key1: Any?,
    key2: Any?,
    key3: Any?,
    effect: DisposableEffectScope.() -> DisposableEffectResult
): Unit
```

A side effect of composition that must run for any new unique value of `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)`, `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` or `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` and must be reversed or cleaned up if `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)`, `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` or `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` changes, or if the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` leaves the composition.

A `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)`'s *key* is a value that defines the identity of the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)`. If a key changes, the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` must `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope#onDispose(kotlin.Function0)` its current `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` and reset by calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` again. Examples of keys include:

- Observable objects that the effect subscribes to

- Unique request parameters to an operation that must cancel and retry if those parameters change

`https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` may be used to initialize or subscribe to a key and reinitialize when a different key is provided, performing cleanup for the old operation before initializing the new. For example:

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

@Composable
fun UserProfile(userRepository: UserRepository, userRequest: UserDataRequest) {
    var userDataState by remember { mutableStateOf<UserDataState>(UserDataState.Loading) }

    // If either the repository or request change, we must cancel our old data fetch
    // and begin a new data fetch. We will cancel the current data fetch if UserProfile
    // leaves the composition.
    DisposableEffect(userRepository, userRequest) {
        val requestDisposable =
            userRepository.fetchUserData(
                userRequest,
                onSuccess = { response -> userDataState = UserDataState.UserData(response) },
                onError = { throwable ->
                    userDataState = UserDataState.Error(throwable.message)
                },
            )

        onDispose { requestDisposable.dispose() }
    }

    // ...
}
```

A `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` **must** include an `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope#onDispose(kotlin.Function0)` clause as the final statement in its `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` block. If your operation does not require disposal it might be a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/SideEffect.composable#SideEffect(kotlin.Function0)` instead, or a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/LaunchedEffect.composable#LaunchedEffect(kotlin.coroutines.SuspendFunction1)` if it launches a coroutine that should be managed by the composition.

There is guaranteed to be one call to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope#onDispose(kotlin.Function0)` for every call to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)`. Both `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` and `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope#onDispose(kotlin.Function0)` will always be run on the composition's apply dispatcher and appliers are never run concurrent with themselves, one another, applying changes to the composition tree, or running `https://developer.android.com/reference/kotlin/androidx/compose/runtime/RememberObserver` event callbacks.

### DisposableEffect

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/Effects.kt+function:DisposableEffect)

```
@Composable
@NonRestartableComposable
fun DisposableEffect(
    key1: Any?,
    key2: Any?,
    key3: Any?,
    key4: Any?,
    effect: DisposableEffectScope.() -> DisposableEffectResult
): Unit
```

A side effect of composition that must run for any new unique value of `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)`, `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)`, `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` or `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` and must be reversed or cleaned up if `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)`, `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)`, `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` or `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` changes, or if the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` leaves the composition.

A `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)`'s *key* is a value that defines the identity of the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)`. If a key changes, the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` must `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope#onDispose(kotlin.Function0)` its current `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` and reset by calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` again. Examples of keys include:

- Observable objects that the effect subscribes to

- Unique request parameters to an operation that must cancel and retry if those parameters change

`https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` may be used to initialize or subscribe to a key and reinitialize when a different key is provided, performing cleanup for the old operation before initializing the new. For example:

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

@Composable
fun UserProfile(userRepository: UserRepository, userRequest: UserDataRequest) {
    var userDataState by remember { mutableStateOf<UserDataState>(UserDataState.Loading) }

    // If either the repository or request change, we must cancel our old data fetch
    // and begin a new data fetch. We will cancel the current data fetch if UserProfile
    // leaves the composition.
    DisposableEffect(userRepository, userRequest) {
        val requestDisposable =
            userRepository.fetchUserData(
                userRequest,
                onSuccess = { response -> userDataState = UserDataState.UserData(response) },
                onError = { throwable ->
                    userDataState = UserDataState.Error(throwable.message)
                },
            )

        onDispose { requestDisposable.dispose() }
    }

    // ...
}
```

A `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffect.composable#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` **must** include an `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope#onDispose(kotlin.Function0)` clause as the final statement in its `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` block. If your operation does not require disposal it might be a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/SideEffect.composable#SideEffect(kotlin.Function0)` instead, or a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/LaunchedEffect.composable#LaunchedEffect(kotlin.coroutines.SuspendFunction1)` if it launches a coroutine that should be managed by the composition.

There is guaranteed to be one call to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope#onDispose(kotlin.Function0)` for every call to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)`. Both `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#DisposableEffect(kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Any,kotlin.Function1)` and `https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisposableEffectScope#onDispose(kotlin.Function0)` will always be run on the composition's apply dispatcher and appliers are never run concurrent with themselves, one another, applying changes to the composition tree, or running `https://developer.android.com/reference/kotlin/androidx/compose/runtime/RememberObserver` event callbacks.