Using CompositionLocal
CompositionLocal
is a Jetpack Compose API that allows you to pass down data to the UI tree, without having to explicitly call them. Instead of passing that data on each step of the hierarchy tree, you can use default values set with CompositionLocal
. This will make your code cleaner and more concise. To access the current value of CompositionLocal
, you can call CompositionLocal.current
and use it in your composable.
The MaterialTheme
composable from the Material Compose SDK, for example, uses CompositionLocal
to provide default color schemes, shapes, and typography values. This makes it easy to access the theme values on any composable in your app. You can simply access a typography text style by calling the following:
MaterialTheme.typography.headlineLarge
MaterialTheme.typography
uses a CompositionLocal
instance to pass the typography set on the theme.
Jetpack Compose has existing CompositionLocal
instances that are available for you to use...