@Composable
fun App() {
var count by remember { mutableStateOf(0) }
Column {
Button(onClick = { count++ }) {
Text("Counter: $count")
}
Child(count)
}
}
@Composable
fun Child(count: Int) {
fun onClick() {
println("Current counter: $count")
}
Column {
Button(onClick = { onClick() }) {
Text("Print with lambda")
}
Button(onClick = ::onClick) {
Text("Print with function reference")
}
}
}
按计数器按钮至少一次后,两个打印按钮的行为不同。 “使用 lambda 打印”按钮打印当前计数器,而“使用函数引用打印”按钮始终打印初始状态 0。 这感觉真的很不直观。这是有意的吗?如果是,是否有任何文档描述此行为?
正如 BenjyTec 所指出的,此行为已在 Compose 1.7.0 中得到修复。