Is there some kind of pattern in Compose to execut...
# compose
o
Is there some kind of pattern in Compose to execute actions inside nested components without exposing too much details outside? In my specific example I have a LazyColumn somewhere deep into a more complex component, and I want to expose an action like “scroll to top” to the outside world. Obviously, I don’t want
LazyListState
to be exposed, because composition of this bigger component can change, but semantic would remain. How do you do this?
a
You can create your own state class, e.g.
MyComponentState
, and expose a function such as
scrollToTop()
.
1
o
And how the function would be implemented? Consumer of MyComponent would need to rememberMyComponentState() and pass it down, and without knowing the internal structure how it would hook in?
a
Yep. You can just hold a
LazyListState
as an internal var and call
LazyListState.scrollToItem()
in
scrollToTop()
.
☝🏻 1