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


<a id='references'></a>


<h2 id="compositionlocalof-policy-defaultfactory">compositionLocalOf</h2>

<div class='sourceset sourceset-common'>Common</div>


```kotlin
public fun <T> compositionLocalOf(
    policy: SnapshotMutationPolicy<T> = structuralEqualityPolicy(),
    defaultFactory: () -> T,
): ProvidableCompositionLocal<T>
```


Create a `CompositionLocal` key that can be provided using `CompositionLocalProvider`. Changing
the value provided during recomposition will invalidate the content of `CompositionLocalProvider`
that read the value using `CompositionLocal.current`.

`compositionLocalOf` creates a `ProvidableCompositionLocal` which can be used in a a call to
`CompositionLocalProvider`. Similar to `MutableList` vs. `List`, if the key is made public as
`CompositionLocal` instead of `ProvidableCompositionLocal`, it can be read using
`CompositionLocal.current` but not re-provided.

#### Parameters

| | |
| --- | --- |
| policy | a policy to determine when a `CompositionLocal` is considered changed. See `SnapshotMutationPolicy` for details. |
| defaultFactory | a value factory to supply a value when a value is not provided. This factory is called when no value is provided through a `CompositionLocalProvider` of the caller of the component using `CompositionLocal.current`. If no reasonable default can be provided then consider throwing an exception. |