KotlinLeaner
11/16/2023, 12:49 PMderivedStateOf
but it not working. Instead if I used simple condition logic it working fine.@Preview(showBackground = true)
@Composable
fun Experiment() {
Surface {
var count by remember { mutableStateOf(0) }
ExperimentView(count) {
count++
}
}
}
@Composable
fun ExperimentView(count: Int, onCountChange: () -> Unit) {
Column {
val floatValue by remember {
derivedStateOf {
if (count % 2 == 0) {
0.4F
} else {
0.2F
}
}
}
Text(text = "$count with float value $floatValue")
Spacer(modifier = Modifier.height(20.dp))
Button(onClick = { onCountChange() }) {
Text(text = "Button")
}
}
}
derivedStateOf
. If I removed derivedStateOf
it working fine. I am adding working example in below
@Composable
fun ExperimentView(count: Int, onCountChange: () -> Unit) {
Column {
val floatValue =
if (count % 2 == 0) {
0.4F
} else {
0.2F
}
Text(text = "$count with float value $floatValue")
Spacer(modifier = Modifier.height(20.dp))
Button(onClick = { onCountChange() }) {
Text(text = "Button")
}
}
}
So why this is happening in this code? ThanksStylianos Gakis
11/16/2023, 12:51 PMMutableState
, not just any value. In your case, your “count” variable is count: Int
not count: MutableState<Int>
.KotlinLeaner
11/16/2023, 12:52 PMStylianos Gakis
11/16/2023, 12:52 PM@Composable
fun ExperimentView(count: Int, onCountChange: () -> Unit) {
val countState by rememberUpdatedState(count)
Column {
val floatValue by remember {
derivedStateOf {
if (countState % 2 == 0) {
0.4F
} else {
0.2F
}
}
}
Text(text = "$count with float value $floatValue")
Spacer(modifier = Modifier.height(20.dp))
Button(onClick = { onCountChange() }) {
Text(text = "Button")
}
}
}
And that should work already, as now you’d be reading from a MutableState
inside your derivedStateOf
Zach Klippenstein (he/him) [MOD]
11/16/2023, 8:51 PMcount
were a snapshot state object, it wouldn’t make sense to use derivedStateOf
here because the calculation is very simple, and the overhead of derivedStateOf
would not be worth it.KotlinLeaner
11/20/2023, 8:27 AM