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

Colton Idle

12/28/2021, 2:25 AM
In the new UI Events section of the Android arch docs (https://developer.android.com/jetpack/guide/ui-layer/events#compose_1) it uses rememberUpdatedState. Why is that needed? It seems like the example works fine without it.
Copy code
val currentOnUserLogIn by rememberUpdatedState(onUserLogIn)
f

Francesc

12/28/2021, 5:25 AM
Without it the launched effect could be holding onto a stale reference of the callback, as it captures the lambda passed in initially. With the updated state it gets updated without restarting the effect
c

Colton Idle

12/28/2021, 5:44 AM
Should I be doing that for all of my events being passed in? so many questions. i guess ill have to play around with this more.
z

Zoltan Demant

12/28/2021, 7:08 AM
Im not sure I can explain this properly, but I think not doing this can lead to some subtle (and not subtle) bugs. One I ran into a while back was my callback referencing an old state, my state was a tad bit more complex - but you can consider a counter that would just move from 0 to 1 instead of continiously incrementing, since it would always look at the initial state, which is 0.
f

Francesc

12/28/2021, 7:24 AM
If you are capturing a callback in an effect then you should use updatedState.
👍 1
Provided updates to the callback should not restart the effect