Jojo C
11/06/2024, 7:05 AM@Preview(showBackground = true)
@Composable
private fun TogglePreview() {
val (checked, onCheckedChange) = remember { mutableStateOf(false) }
val (checked2, onCheckedChange2) = remember { mutableStateOf(false) }
val (checked3, onCheckedChange3) = remember { mutableStateOf(false) }
Column() {
Switch(checked, onCheckedChange)
Switch(checked2, onCheckedChange2)
Switch(checked3, onCheckedChange3)
}
}
Here is the minimal producible code.curioustechizen
11/06/2024, 7:08 AMAnton Poliakov
11/06/2024, 7:25 AMJojo C
11/06/2024, 7:28 AMvar checked by remember { mutableStateOf(false) }
And when you try to pass in the lambda from Switch you use
Switch(checked, {checked = it})
Jojo C
11/06/2024, 7:30 AMval (checked, onChecked) = remember { mutableStateOf(false) }
And in this case you won't need to do the {checked = it}
Because the onChecked itself is already a lambdaAnton Poliakov
11/06/2024, 7:30 AMJojo C
11/06/2024, 7:30 AMChrimaeon
11/06/2024, 7:41 AMStylianos Gakis
11/06/2024, 8:33 AMby
.
https://kotlinlang.slack.com/archives/CJLTWPH7S/p1659553222689339?thread_ts=1659213420.545199&cid=CJLTWPH7S
When you use that, you are forcing a read on the declaration itself, completely losing out on any potential wins of deferring the read to a smaller scope and therefore improving performance.
I see absolutely 0 reason to use it, and I am honestly surprised why it still is not being discouraged as an approach.
Read this https://blog.zachklipp.com/scoped-recomposition-in-jetpack-compose-what-happens-when-state-changes/ for some more details on this. In that example for instance, if you used destructuring you'd then be recomposing everything, including the button due to reading the state at a higher recomposition scope than you would otherwise.Jojo C
11/06/2024, 9:01 AM