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

Sultan

11/02/2023, 2:50 AM
Is there any way to pass data from view in lower to upper compositional hierarchy e.g we can pass data down through the Composition implicitly using compositionalLocal but can we pass data UP using compositionalLocal or other ? what you suggest.
k

Kirill Grouchnikov

11/02/2023, 3:07 AM
Why would a lower level view own a piece of data to begin with
Also composition locals are very much not for passing data down the tree
s

Sultan

11/02/2023, 3:09 AM
working on a project that changes view components depending on the data in other view
k

Kirill Grouchnikov

11/02/2023, 3:10 AM
Right, but in compose and in reactive frameworks in general, views don’t own data
s

Sultan

11/02/2023, 3:12 AM
So best way is to use view model I think ?
k

Kirill Grouchnikov

11/02/2023, 3:14 AM
Yes. Data goes down, events bubble up. The place that owns the data is also responsible for updating that data based on these events. And then Compose takes care of updating the parts of the tree that should be updated based on whatever changed in the data.
💯 4
s

Sultan

11/02/2023, 3:14 AM
In my project we already have view model for each screen
k

Kirill Grouchnikov

11/02/2023, 3:15 AM
So if place A in your app can change the data X, it doesn't change it directly. It bubbles up the change with whatever callback you push down to it, and then that change gets propagated to place B in your app that shows X in the UI.
s

Sultan

11/02/2023, 3:18 AM
correct but that A is the one level down from B where need to read that data
k

Kirill Grouchnikov

11/02/2023, 3:19 AM
Data ownership and modification is at the root level, not in A and not in B
👀 1
👆 2
👆🏾 1
👍 1
2 Views