Prateek Kumar
07/25/2023, 10:06 PMPablichjenkov
07/25/2023, 11:59 PMremember
and mutableState
are 2 separate things. remember
just keep alive the same instance of whatever you pass through the different recomposition cycles. If the remember function position in the composable tree does not change in the recomposition it keeps the same value passed before.
Even if you pass another value from the composable function parameters it will keep the initial. To change this behavior and being able to update the remembered value you use a key, to instruct remember that it needs to update when the key updates.
In general using keys is a good practice
MutableState, is the actual element that causes recomposition, remember has nothing to do with triggering recomposition. MutableState always triggers recomposition when the value it enclosed changes.Timo Drick
07/26/2023, 1:37 PMrememberUpdateState(key)
in stead of remember(key) { mutableStateOf(key) }
. But i am also a little bit unsure. Here is the documentation: https://developer.android.com/jetpack/compose/side-effects#rememberupdatedstate
Maybe some one knows more?Pablichjenkov
07/26/2023, 2:29 PMPrateek Kumar
07/26/2023, 5:37 PMPrateek Kumar
07/26/2023, 5:39 PMPablichjenkov
07/26/2023, 6:51 PMtabPosition
, from what scope. It might be the case of a stale reference to a mutableState. When the remember key changes it will give you a new reference to a new MutableState instance. Are you sure you update your lambdas with references to this new MutableState and not keep the references to the old instance. Calling on the old one won’t have effect(won’t cause recomposition).Prateek Kumar
07/26/2023, 8:34 PMvar tabPosition by remember(items) { mutableStateOf(if (currentMcqPosition <= 0) 0 else currentMcqPosition / STEP_SIZE) }
LaunchedEffect(state) {
snapshotFlow { state.firstVisibleItemIndex }.collect {
if (state.firstVisibleItemIndex == 0) return@collect
tabPosition = (state.firstVisibleItemIndex / STEP_SIZE)
}
}
But even then i feel this problem should not happen ,
If we use a remember with key and if we are changing that from some LaunchedEffect or anywhere else, should still work
Now it looks like i will have to fall back to rememberUpdatedState for this.Pablichjenkov
07/26/2023, 9:00 PMPrateek Kumar
07/27/2023, 4:46 AMPablichjenkov
07/27/2023, 4:53 AMPablichjenkov
07/27/2023, 4:57 AMPrateek Kumar
07/27/2023, 5:03 AM