Any time a state is updated a recomposition takes place.
但是在这里,我没有使用 remember API,但是在重组之后它也保存了值,是 mutableStateOf()
会记住没有 remember
的值API?
@Composable
fun MyChildUI() {
var count by mutableStateOf(1)
Button(onClick = {
count++
println(count)
}) {
Text(text = "$count")
}
}
最佳答案
这是因为作用域重组。任何非内联且返回 Unit 的 Composable 都是一个范围。 Compose 仅触发最近范围内的重组。在您的示例中,它是 Button 的范围。你可以看看这个非常相似的问题
关于kotlin - mutableStateOf 保留重组后的值,而无需记住 Jetpack Compose 中的 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73760697/