Composition Local : Fundamentals - devrath/ComposeAlchemy GitHub Wiki
What is Composition Local used for?
- This is a way for multiple composable to access the data.
Background
- Composables are the hierarchy of
UI
placed in a certain structure and data is passed to certain parts
- So the composables reflect a
state of data
- But say you have some composables in the hierarchy and several of them have the same data, This might become complex.
- So with the help of
composition local
we can make it simpler so that several composables can simply share the same data.
Getting to know the composition-Local
- Data flows through different composables from one to the next so that each composable knows exactly what
data/dependencies
it needs. -> Now this is useful for specific composables that are not used elsewhere.
- There are times when we need to use multiple composables in the
UI tree
that need the same data. Passing the same data among the compostables multiple times may be inconvenient.
What composition local does
- It creates an object which is available through the
UI-tree
.
- Now we can make it such a way that this object is available to any composable in the UI tree of a subset of it.
Some composition locals examples
- To get the context in a composable
val context = LocalContext.current
val keyboardController = LocalSoftwareKeyboardController.current