Thread
#compose
    Tash

    Tash

    1 year ago
    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:
    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 🙏🏼
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    1 year ago
    Any reason you’re not using a lazy list?
    Tash

    Tash

    1 year ago
    Trying to create these inside of a
    Box
    to achieve a stack
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    1 year ago
    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
    Tash

    Tash

    1 year ago
    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 🤔
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    1 year ago
    then I think that that if/else should just work
    Tash

    Tash

    1 year ago
    Oh I see
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    1 year ago
    I don’t think it needs to be in an effect, assuming firstItemState/lastItemState are MutableStates – 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 MutableStates, then i think they’d need to be in an effect
    Tash

    Tash

    1 year ago
    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!