martmists
09/23/2024, 10:33 AMmutableStateListOf
? I have a DisposableEffect(someStateList)
but it doesn't get re-triggered when I add to/remove from the list. What's the intended use-case?shikasd
09/23/2024, 1:27 PMsomeStateList.toList
hereZach Klippenstein (he/him) [MOD]
09/23/2024, 4:40 PMtoList
, then why not just store an immutable list in a mutableStateOf
?martmists
09/23/2024, 5:02 PMZach Klippenstein (he/him) [MOD]
09/23/2024, 5:17 PMI was hoping mutableStateListOf was a way to handle mutations to a MutableList in ComposeIt is.
Zach Klippenstein (he/him) [MOD]
09/23/2024, 5:20 PMDisposableEffect(someStateList) { <-- This doesn't read the list and the instance is always the same, so the effect will never restart.
someStateList.forEach { … } <-- This will read the current value of the list, but not restart.
}
Then you have 2 options. First, what Andrei suggested:
DisposableEffect(someStateList.toList()) { <-- This is now a state read and will return a different instance when it changes.
someStateList.forEach { … }
}
And if you want to avoid recomposing entirely:
LaunchedEffect(someStateList) {
snapshotFlow { <-- This lambda will restart whenever the list changes.
someStateList.forEach { … }
}.collect()
}