Android Programming: The Big Nerd Ranch Guide, 5th Edition
by Bryan Sills, Brian Gardner, Kristin Marsicano, Chris Stewart
For the More Curious: Creating Your Own CompositionLocals
In this chapter, you learned about several built-in CompositionLocals and used the LocalContext to obtain a Context in your composable. If you want, you can also define your own CompositionLocals.
Declaring a CompositionLocal is particularly useful when you want to give your composables access to new values without introducing an additional parameter. This works best when the information being provided applies to many composables and can be shared across an entire section of your composition hierarchy. Much like global variables, CompositionLocals can be dangerous if used haphazardly. If a value should only be available to one composable, we recommend sticking to parameters. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access