Trying to render a list of items, each with their ...
# compose
t
Trying to render a list of items, each with their own 
State
, and would like to keep track of the first & last item’s 
State
. For some reason having trouble wrapping my mind around this particular scenario 🧵 ⬇️
I’m sure there’s a simple solution to this, but my mind is rejecting Compose-style thinking today 🙈 . Probably need more . Here’s some pseudocode of what I’m trying to achieve:
Copy code
items.forEachIndexed { index, item ->
   val itemState = rememberItemState(key = item.id, /** etc **/)
   
   // Somehow do this
   if (index == items.size - 1) {
      lastItemState.value = itemState
   } else if (index == 0) {
      firstItemState.value = itemState
   }

   key(item.id) { Item(state = itemState, content = content) }
}
Any insight is much appreciated 🙏🏼
z
Any reason you’re not using a lazy list?
t
Trying to create these inside of a
Box
to achieve a stack
z
ah
you probably want to put all that inside the
key
block, that’s what will ensure that an item’s memoized state will move along with the item if the order of the list changes
t
Ahh, thats a good point, thank you
I was trying to understand if the
if/else
in there needs to happen in some type of
Effect
block or not 🤔
z
then I think that that if/else should just work
t
Oh I see
z
I don’t think it needs to be in an effect, assuming `firstItemState`/`lastItemState` are `MutableState`s – because they participate in the snapshot system, if the composition fails, those writes will be rolled back, so they don’t need to be done in an effect.
If they weren’t `MutableState`s, then i think they’d need to be in an effect
t
Ah yes of course. They are indeed
MutableState
s, so that should work. I’ll try moving everything into the
key
block and keep an eye on state integrity. Thank you loads!