Hey, I've noticed that a change of value of `Mutab...
# compose
m
Hey, I've noticed that a change of value of
MutableState
leads to recomposition to all the composable functions that read this state, but I cannot find how exactly that is triggered. Like in this simple code, clicks only cause "Recompose BodyReading" and "Value: true/false".
Copy code
@Preview
@Composable
fun Recomposition() {
    val value = remember { mutableStateOf(false) }
    println("Recompose Recomposition")
    Button(onClick = { value.value = !value.value }) {
        Text("Click me")
    }

    BodyReading(value)
    BodyWriting(value)
}

@Composable
fun BodyReading(value: MutableState<Boolean>) {
    println("Recompose BodyReading")
    println("Value: ${value.value}")
    Text(" ")
    Text(" ")
}

@Composable
fun BodyWriting(value: MutableState<Boolean>) {
    println("Recompose BodyNotReading")
    value.value = false
    Text(" ")
    Text(" ")
}
In the compiled code, I do not see any essential differences, except that the first one has additional print and
value.getValue()
, while the other one
value.setValue(false)
. They both start
startRestartGroup
. My assumption is that
getValue
sets some listener for recomposition, but I cannot find that, and I would love to understand how that works.
s
m
Thanks, this is exactly what I was looking for. I suspected this line of code, but now I see how it works 👍
👍 3