Colton Idle
08/14/2025, 10:25 PMremember
arg
I have
itemsIndexed(items) { index, item ->
val thing = remember { MyThing(item) }
}
teammate says this should be
val thing = remember(item) { MyThing(item) }
instead of my approach
val thing = remember() { MyThing(item) }
Colton Idle
08/14/2025, 10:32 PMagrosner
08/14/2025, 10:32 PMColton Idle
08/14/2025, 10:32 PMagrosner
08/14/2025, 10:32 PMagrosner
08/14/2025, 10:32 PMColton Idle
08/14/2025, 10:34 PMagrosner
08/14/2025, 10:36 PMColton Idle
08/14/2025, 10:39 PMitem
that's passed in.agrosner
08/14/2025, 10:43 PMagrosner
08/14/2025, 10:44 PMagrosner
08/14/2025, 10:44 PMeygraber
08/15/2025, 2:23 AMMyThing
will be recomputed if the item or the item's position changes. If not, then the same MyThing
will be returned regardless of the item at the current index.Colton Idle
08/15/2025, 2:43 AMval thing = remember(item) { MyThing(item) }
== MyThing will be recomputed if the item or the item's position changes
val thing = remember() { MyThing(item) }
== the same MyThing will be returned regardless of the item at the current index
?Albert Chang
08/15/2025, 2:47 AMitemIndexed
), otherwise the remembered MyThing
can be different from the actual item after list items change.Colton Idle
08/15/2025, 2:48 AMAlbert Chang
08/15/2025, 2:49 AMitemIndexed
is the better way as it prevents the need to recreate MyThing
.Colton Idle
08/15/2025, 2:50 AMColton Idle
08/15/2025, 2:51 AMColton Idle
08/15/2025, 2:52 AMitemsIndexed(items) { index, item ->
val thing = remember { MyViewModel(item) }
}
To which we learned it should instead be
itemsIndexed(items, { index, item -> item.id }) { index, item ->
val thing = remember { MyViewModel(item) }
}
Colton Idle
08/15/2025, 2:56 AMMyViewModel
and into MyActualScreensViewModel
and therefore I won't have this issue of creating this thing on the fly. But overall I think I learned something here... so thank you all!shikasd
08/15/2025, 11:59 AMitemsIndexed
is the lazy list extension, then your thing works only when the key is specified, because compose reset composition completely when reusing itemsshikasd
08/15/2025, 12:00 PMremember
value, as commenter above mentionedshikasd
08/15/2025, 12:02 PMremember(item)
. Using remember
without keys is only technically correct in lazy since you can guarantee that only one item
is going to be presentColton Idle
08/15/2025, 12:36 PMif itemsIndexed is the lazy list extensionyes this is in a lazyColum
if the key is not specified, it is equivalent to using item index as a key🤯
Using remember without keys is only technically correct in lazy since you can guarantee that only one item is going to be present🤯 Why do I feel like I've been using remember all wrong for the past 5 years. lol
Colton Idle
08/15/2025, 12:46 PMColton Idle
08/15/2025, 12:56 PMitemsIndexed(items) { index, item ->
val thing = remember { MyViewModel(item) }
Column {
Text(item.toString())
Text(this.currentStopWatchValue())
}
}
Colton Idle
08/15/2025, 1:46 PMval thing = remember(KEY) { MyViewModel(item) }
it did not work.shikasd
08/15/2025, 1:59 PMColton Idle
08/15/2025, 2:24 PM