Abhinav Suthar
11/17/2021, 10:24 AMSnapshotStateList<Char>
to a composable function. But any changes to SnapshotStateList's item(s)
doesn’t re-trigger the LaunchedEffect
Any idea why?
Code in thread.Abhinav Suthar
11/17/2021, 10:24 AM@Composable
fun Foo() {
val otpCode = remember {
List(5) {' '}.toMutableStateList()
}
OTPTextField(code = otpCode)
ButtonPrimary(text = "Some Button, ${otpCode.joinToString()}") {
otpCode[0] = '1'
}
}
@Composable
fun OTPTextField(
code: SnapshotStateList<Char>,
) {
LaunchedEffect(code) {
// this does not trigger on recomposition (caused by change in SnapshotStateList's item value) i.e, Button click
Log.d("TAG", code.joinToString())
}
}
Abhinav Suthar
11/17/2021, 10:32 AMSome Button, ${otpCode.joinToString()}
) after I click on the button, but LaunchedEffect
fails to re-triggerZach Klippenstein (he/him) [MOD]
11/17/2021, 5:09 PMequals
and for lists that's just a reference comparison, not a structural one. You could use snapshotFlow
for this. Eg:
LaunchedEffect(code) {
snapshotFlow { code.joinToString() }
.collect { Log.d(“TAG”, it) }
}
Note that you should still pass the list as a key, since the effect should restart if a different list instance is passed.Abhinav Suthar
11/18/2021, 9:40 AMZach Klippenstein (he/him) [MOD]
11/18/2021, 5:21 PM