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

Julius Marozas

08/17/2020, 9:49 AM
Can I set a custom key (instead of element index) in
LazyColumnFor
composable?
z

Zach Klippenstein (he/him) [MOD]

08/17/2020, 3:39 PM
I thought
LazyColumnFor
used the element itself as the key, unless that changed recently.
j

Julius Marozas

08/17/2020, 3:46 PM
It probably has then. Here is the source.
So should I just copy
LazyFor
composable (since it's private) and use a custom key? Or is there another way?
z

Zach Klippenstein (he/him) [MOD]

08/17/2020, 3:52 PM
What do you need a custom key for? Can you not just put the keys you want in the list?
j

Julius Marozas

08/17/2020, 3:57 PM
I have a list of items that have unique ids. I want to avoid recomposing the entire tree under
LazyColumnFor
when I insert a new item at the beginning of the list. So I was hoping that I could solve this by specifying the key as the id of the item (similarly to how it is usually done in React).
z

Zach Klippenstein (he/him) [MOD]

08/17/2020, 4:04 PM
You could do a couple things: 1. Just pass a list of IDs, and look up the actual element in a map. 2. Either change the definition of
equals
for your element type to only consider ID, or wrap your values in a type that does this. Either way, I would file a feature request issue to the compose project to ask for this feature (tracked link is in channel topic/description)
Both of those solutions potentially require creating new lists on every composition, which isn't ideal.
j

Julius Marozas

08/17/2020, 5:01 PM
Thanks for the suggestions! I have filed an issue.
👍 2
2 Views