In the new UI Events section of the Android arch d...
# compose
c
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
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
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
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
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