Having a list detail screen with an `uistate`, it'...
# compose
p
Having a list detail screen with an
uistate
, it's recommended to store only the ID of a selected item in the
uistate
instead of the full object. So... where should we put the logic that iterates and gets the full object from the array using the id? My first thought was to put it on the
viewmodel
, but the
viewmodel
is hoisted to upper composables and is not possible to access it on the composable that receives the uistate.
v
Again, there are many different options 😄
(It's not a rule written in stone that you shouldn't store the object either.)
One option is to expose a derived state from a viewmodel or other state object:
Copy code
val selectedItem = derivedStateOf { allOfMyItems[selectedIndex] }
Another is to just store the selected one directly. Or expose all items and the index as far down in the hierarchy as you need them