AmrJyniat
11/20/2021, 6:53 AMScaffoldState
work with CompositionLocalProvider
to control the drawer open/close from anywhere in my fragment?Bradleycorn
11/21/2021, 2:34 PMCompositionLocal
for something like that. Instead, pass a callback function down the compose tree. When you have some ui element that needs to open/close the drawer, it can call the callback and your top level composable that owns the ScaffoldState can update it to toggle the drawerAmrJyniat
11/21/2021, 4:57 PMCompositionLocal
created in core to avoid passing callbacks all the way down tree.Bradleycorn
11/22/2021, 12:37 PMCompositionLocal
.
As developers, I think we sometimes think of callback functions as a bad thing (probably because of the proliferation of the term “callback hell”). But they aren’t always bad. And with compose I think we have to get used to the idea of passing Ambient
to CompositionLocal
, they included the word “Local” in the name on purpose to clearly indicate that we probably should not use it in a Global fashion. And that’s what we’d be doing in this case. It’s setting up a CompositionLocal
that is not local at all, and works like a Global variable used to control a navigation drawer.AmrJyniat
11/22/2021, 1:16 PMCompositionLocal
actually right, but here I wouldn't re-use the fun in anywhere else because it's a drawer which exists only in home screen so I guess I can benefit from CompositionLocal
in such cases.