https://kotlinlang.org logo
#compose
Title
# compose
o

orangy

06/12/2022, 12:04 PM
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

Albert Chang

06/12/2022, 2:09 PM
You can create your own state class, e.g.
MyComponentState
, and expose a function such as
scrollToTop()
.
1
o

orangy

06/12/2022, 3:32 PM
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

Albert Chang

06/12/2022, 11:39 PM
Yep. You can just hold a
LazyListState
as an internal var and call
LazyListState.scrollToItem()
in
scrollToTop()
.
☝🏻 1