Function

compositionLocalOf

Create a CompositionLocal key that can be provided using CompositionLocalProvider.

compositionLocalOf

Source set: Common
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.
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: