Recently I faced a problem with remember
in compose; I have two simple, composable functions like the below:
@Composable
fun foo() {
var x = remember { 1 }
x += 1
Text("$x")
}
@Composable
private fun MainScreen() {
var state by remember { mutableStateOf(1) }
Column {
foo()
Button(onClick = { state += 1 }) {
Text("Update Offer $state")
}
}
}
I expect that when clicking the button occurs, foo()
recompose, and x
increases, but this isn't work, and the x
is always 2. Can anyone explain why it doesn't work as I expected?