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

Shakil Karim

01/31/2022, 6:36 AM
Hi, i was looking at JetNews source code, Can someone please help me understand the need of key here, is there any downside if don't use it? https://github.com/android/compose-samples/blob/84dc6417dfbd6408f498d71afcc1272087[…]ws/app/src/main/java/com/example/jetnews/ui/home/HomeScreens.kt
j

jasu

01/31/2022, 6:52 AM
Its mostly for performance improvement. Basically prevents compose not to recompose on every recomposition. Works as identifier for a particular item to identify it later on time
s

Shakil Karim

01/31/2022, 7:16 AM
Thanks, but doesn't LazyColumn under Key do the same, if state is not changed it will not recompose ?
j

jasu

01/31/2022, 7:25 AM
yes it does, but in given context only thing I can conclude is they want the down tree to recompose when post id changes not otherwise
👍 1
a

Albert Chang

01/31/2022, 10:22 AM
This has nothing to do with performance. The reason is explained in the comment above, i.e. to recreate a completely new
LazyColumn
when the post id changes.
☝️ 1
s

Shakil Karim

01/31/2022, 11:02 AM
Thanks @Albert Chang When do you think we need a new
LazyColumn
? I mean isn't changing the State of
LazyColumn
will be enough?
a

Albert Chang

01/31/2022, 11:06 AM
Without the
key
, states of the items in it such as
PostTopBar
and
postContentItems
will not change even if the
LazyListState
changes.
👍 1
s

Shakil Karim

01/31/2022, 11:13 AM
Ah! Got it Thanks 👍
3 Views