在 compose 中使用函数引用而不是 lambda 时的状态已过时

问题描述 投票:0回答:1
@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。 这感觉真的很不直观。这是有意的吗?如果是,是否有任何文档描述此行为?

kotlin android-jetpack-compose compose-multiplatform
1个回答
0
投票

正如 BenjyTec 所指出的,此行为已在 Compose 1.7.0 中得到修复。

© www.soinside.com 2019 - 2024. All rights reserved.