theapache64
12/05/2022, 8:07 PMdata class Car(val name: String)
@Composable
private fun Test() {
var car by remember { mutableStateOf(Car("Audi")) }
var remCar = remember { car }
LaunchedEffect(car) {
remCar = car
}
println("remCar is `${remCar.name}`")
Button(
onClick = {
car = Car("BMW - ${System.currentTimeMillis()}")
}
) {
Text("CHANGE CAR")
}
}
Pressing CHANGE CAR button always prints remCar is Audi
Why it’s not printing BMW ? 🤔jw
12/05/2022, 8:09 PMjw
12/05/2022, 8:10 PMjw
12/05/2022, 8:10 PMcar as a key to the second rememberjw
12/05/2022, 8:11 PMLaunchedEffect, but it's not a State so writes to it presumably won't trigger recompositiontheapache64
12/05/2022, 8:15 PMBMW for the next recomposition, right?jw
12/05/2022, 8:16 PMremember will re-read the original car value from the slot table and it will always reset to the Auditheapache64
12/05/2022, 8:17 PMremember -ed value like thisjw
12/05/2022, 8:18 PMKevin Del Castillo
12/05/2022, 8:18 PMremCar is not actually changing the value of the mutableStateOf , that's a misconception because the setter of car is not actually just setting a variable but updating the state, also you don't need to remember car , it's been already remembered.jw
12/05/2022, 8:19 PMState the backing storage contains a reference to the State instance rather than a raw value. This is why changes to the State are reflected on future recompositions. The remember reads out the same State instance each time but the value held by that State is what changes.theapache64
12/05/2022, 8:21 PM