Can I set a custom key (instead of element index) ...
# compose
j
Can I set a custom key (instead of element index) in
LazyColumnFor
composable?
z
I thought
LazyColumnFor
used the element itself as the key, unless that changed recently.
j
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
What do you need a custom key for? Can you not just put the keys you want in the list?
j
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
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
Thanks for the suggestions! I have filed an issue.
👍 2