theapache64
06/13/2023, 10:04 PM@Composable
fun MyComposable() {
// Store the value of count using remember
var count by remember { mutableStateOf(0) }
// Compute the derived state based on count
val doubledCount by
derivedStateOf {
println("MainActivity:MyComposable:derivedStateOf")
count * 2
}
Column {
// Update the count when a button is clicked
Button(onClick = { count++ }) {
Text("Increment Count: $count")
}
// Display the derived state
Text("Doubled Count: $doubledCount")
}
}
Even though derivedStateOf
’s body called twice (because we don’t have remember
around it), the value is correct.
• Why (or how?) is this happening?
• Why there’s no infinite recomposition? 👀theapache64
06/13/2023, 10:05 PMshikasd
06/14/2023, 12:29 AMshikasd
06/14/2023, 12:31 AMtheapache64
06/14/2023, 12:42 AM