Rick Regan
02/01/2021, 5:21 PMAdam Powell
02/01/2021, 5:23 PMRick Regan
02/01/2021, 5:24 PM@Composable
fun RecomposeTest() {
var buttonPressed by remember { mutableStateOf(0) }
Row {
for (i in 1 until 5)
Key(i, { buttonPressed = it} )
Text(text = "$buttonPressed")
}
}
@Composable
fun Key(
index: Int,
onClick: (index: Int) -> Unit
) {
println("Key() composed, index: $index")
Button(onClick = { onClick(index) }) {
Text(text = index.toString())
}
}
Zach Klippenstein (he/him) [MOD]
02/01/2021, 6:15 PMKey
which triggers the recompositionRick Regan
02/01/2021, 7:37 PM@Composable
fun RecomposeTest() {
var buttonPressed by remember { mutableStateOf(0) }
fun onClick(index: Int) { buttonPressed = index}
Row {
for (i in 1 until 5)
Key(i, ::onClick)
Text(text = "$buttonPressed")
}
}
What you cite though -- is it an implementation detail, or something that should be a part of a user's mental model?Adam Powell
02/01/2021, 7:47 PMRick Regan
02/13/2021, 10:34 PMZach Klippenstein (he/him) [MOD]
02/14/2021, 12:43 AMLeland Richardson [G]
02/16/2021, 7:17 PMWhat you cite though -- is it an implementation detail, or something that should be a part of a user’s mental model?Generally speaking you shouldn’t have your code rely on a function skipping for correctness. Skipping should ideally only impact performance. If you have side effects that you want to ensure only get run “once” or something like that, you should use *effect APIs, and not execute those side effects during composition. Compose doesn’t guarantee skipping if the parameters are the same. There are conditions where we may execute anyway.
Rick Regan
02/16/2021, 10:04 PMLeland Richardson [G]
02/16/2021, 10:30 PM