# ProvidableCompositionLocal

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/CompositionLocal.kt+class:androidx.compose.runtime.ProvidableCompositionLocal) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0) Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

  ```
  abstract class ProvidableCompositionLocal<T : Any?> : CompositionLocal<T>
  ```

|---|---|---|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) |||
| ↳ | [androidx.compose.runtime.CompositionLocal](https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocal)\<T\> ||
|   | ↳ | [androidx.compose.runtime.ProvidableCompositionLocal](https://developer.android.com/reference/kotlin/androidx/compose/runtime/ProvidableCompositionLocal) |

*** ** * ** ***

A `https://developer.android.com/reference/kotlin/androidx/compose/runtime/ProvidableCompositionLocal` can be used in `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocalProvider.composable#CompositionLocalProvider(kotlin.Array,kotlin.Function0)` to provide values.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#compositionLocalOf(androidx.compose.runtime.SnapshotMutationPolicy,kotlin.Function0)` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#staticCompositionLocalOf(kotlin.Function0)` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocal` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocalProvider.composable#CompositionLocalProvider(kotlin.Array,kotlin.Function0)` |   |

## Summary

| ### Public functions |
|---|---|---|
| `infix https://developer.android.com/reference/kotlin/androidx/compose/runtime/ProvidedValue<T>` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/ProvidableCompositionLocal#provides(kotlin.Any)(value: T)` Associates a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocal` key to a value in a call to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocalProvider.composable#CompositionLocalProvider(kotlin.Array,kotlin.Function0)`. | Cmn |
| `infix https://developer.android.com/reference/kotlin/androidx/compose/runtime/ProvidedValue<T>` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/ProvidableCompositionLocal#providesComputed(kotlin.Function1)(compute: https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocalAccessorScope.() -> T)` Associates a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocal` key to a lambda, `https://developer.android.com/reference/kotlin/androidx/compose/runtime/ProvidableCompositionLocal#providesComputed(kotlin.Function1)`, in a call to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocal`. | Cmn |
| `infix https://developer.android.com/reference/kotlin/androidx/compose/runtime/ProvidedValue<T>` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/ProvidableCompositionLocal#providesDefault(kotlin.Any)(value: T)` Associates a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocal` key to a value in a call to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocalProvider.composable#CompositionLocalProvider(kotlin.Array,kotlin.Function0)` if the key does not already have an associated value. | Cmn |

### Inherited properties

From [androidx.compose.runtime.CompositionLocal](https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocal) `T` `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocal#current()`

Return the value provided by the nearest `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocalProvider.composable#CompositionLocalProvider(kotlin.Array,kotlin.Function0)` component that invokes, directly or indirectly, the composable function that uses this property.
Cmn

## Public functions

### provides

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

```
infix fun provides(value: T): ProvidedValue<T>
```

Associates a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocal` key to a value in a call to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocalProvider.composable#CompositionLocalProvider(kotlin.Array,kotlin.Function0)`.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocal` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/ProvidableCompositionLocal` |   |

### providesComputed

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

```
infix fun providesComputed(compute: CompositionLocalAccessorScope.() -> T): ProvidedValue<T>
```

Associates a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocal` key to a lambda, `https://developer.android.com/reference/kotlin/androidx/compose/runtime/ProvidableCompositionLocal#providesComputed(kotlin.Function1)`, in a call to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocal`. The `https://developer.android.com/reference/kotlin/androidx/compose/runtime/ProvidableCompositionLocal#providesComputed(kotlin.Function1)` lambda is invoked whenever the key is retrieved. The lambda is executed in the context of a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocalContext` which allow retrieving the current values of other composition locals by calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocalAccessorScope#(androidx.compose.runtime.CompositionLocal).currentValue()`, which is an extension function provided by the context for a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocal` key.

The lambda passed to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/ProvidableCompositionLocal#providesComputed(kotlin.Function1)` will be invoked every time the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocal#current()` is evaluated for the composition local and computes its value based on the current value of the locals referenced in the lambda at the time `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocal#current()` is evaluated. This allows providing values that can be derived from other locals. For example, if accent colors can be calculated from a single base color, the accent colors can be provided as computed composition locals. Providing a new base color would automatically update all the accent colors.

```kotlin
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf

val LocalValue = compositionLocalOf { 10 }
val LocalLargerValue = compositionLocalOf { 12 }

@Composable
fun App() {
    CompositionLocalProvider(
        LocalLargerValue providesComputed { LocalValue.currentValue + 10 }
    ) {
        SomeScreen()
    }
}
```

```kotlin
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.compositionLocalWithComputedDefaultOf

val LocalValue = compositionLocalOf { 10 }
val LocalLargerValue = compositionLocalOf { 12 }
val LocalComputedValue = compositionLocalWithComputedDefaultOf { LocalValue.currentValue + 4 }

// In this example `LocalLargerValue` needs to be re-provided
// whenever `LocalValue` is provided to keep its value larger
// then `LocalValue`. However, `LocalComputedValue` does not
// need to be re-provided to stay larger than `LocalValue` as
// it is calculated based on the currently provided value for
// `LocalValue`. Whenever `LocalValue` is provided the value
// of `LocalComputedValue` is computed based on the currently
// provided value for `LocalValue`.

@Composable
fun App() {
    // Value is 10, the default value for LocalValue
    val value = LocalValue.current
    // Value is 12, the default value
    val largerValue = LocalLargerValue.current
    // Value is computed to be 14
    val computedValue = LocalComputedValue.current
    CompositionLocalProvider(LocalValue provides 20) {
        // Value is 20 provided above
        val nestedValue = LocalValue.current
        // Value is still 12 as an updated value was not re-provided
        val nestedLargerValue = LocalLargerValue.current
        // Values is computed to be 24; LocalValue.current + 4
        val nestedComputedValue = LocalComputedValue.current
        CompositionLocalProvider(LocalLargerValue provides LocalValue.current + 2) {
            // Value is 22 provided above
            val newLargerValue = LocalLargerValue.current

            CompositionLocalProvider(LocalValue provides 50) {
                // Value is now 50 provided above
                val finalValue = LocalValue.current
                // Value is still 22
                val finalLargerValue = LocalLargerValue.current
                // Value is now computed to be 54
                val finalComputed = LocalComputedValue.current
            }
        }
    }
}
```

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocal` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocalContext` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/ProvidableCompositionLocal` |   |

### providesDefault

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

```
infix fun providesDefault(value: T): ProvidedValue<T>
```

Associates a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocal` key to a value in a call to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocalProvider.composable#CompositionLocalProvider(kotlin.Array,kotlin.Function0)` if the key does not already have an associated value.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/CompositionLocal` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/ProvidableCompositionLocal` |   |