Hi, i was looking at JetNews source code, Can some...
# compose
s
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
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
Thanks, but doesn't LazyColumn under Key do the same, if state is not changed it will not recompose ?
j
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
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
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
Without the
key
, states of the items in it such as
PostTopBar
and
postContentItems
will not change even if the
LazyListState
changes.
👍 1
s
Ah! Got it Thanks 👍