Jetpack Compose 记得实际上做了什么,它是如何在引擎盖下工作的? [英] What does Jetpack Compose remember actually do, how does it work under the hood?

查看:36
本文介绍了Jetpack Compose 记得实际上做了什么,它是如何在引擎盖下工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看 codelab 的基本教程,有一个片段可以在单击时增加按钮上的计数器

Checking out codelab's basic tutorial there is a snippet to increase counter on button when clicked

@Composable
fun MyScreenContent(names: List<String> = listOf("Android", "there")) {
    val counterState = remember { mutableStateOf(0) }

    Column(modifier = Modifier.fillMaxHeight()) {
        Column(modifier = Modifier.weight(1f)) {
            for (name in names) {
                Greeting(name = name)
                Divider(color = Color.Black)
            }
        }
        Counter(
            count = counterState.value,
            updateCount = { newCount ->
                counterState.value = newCount
            }
        )
    }
}


@Composable
fun Counter(count: Int, updateCount: (Int) -> Unit) {
    Button(
        onClick = { updateCount(count + 1) },
        colors = ButtonConstants.defaultButtonColors(
            backgroundColor = if (count > 5) Color.Green else Color.White
        )
    ) {
        Text("I've been clicked $count times")
    }
}

很明显 remember { mutableStateOf(0) } 存储状态/值.我的问题是记忆在幕后做了什么.使用 var count = remember { 0 }mutableStateOf(0) 而没有 remember 不会增加值.

It is clear that remember { mutableStateOf(0) } stores the state/value. My question is what remember does under the hood. Using var count = remember { 0 } or mutableStateOf(0) without remember does not increase the value.

fun MyScreenContent(names: List<String> = listOf("Android", "there")) {
   
    var count = remember { 0 }

    Column(modifier = Modifier.fillMaxHeight()) {
        Column(modifier = Modifier.weight(1f)) {
            for (name in names) {
                Greeting(name = name)
                Divider(color = Color.Black)
            }
        }
        Counter(
            count = count,
            updateCount = { newCount ->
                count = newCount
            }
        )
    }
}

上面的代码段不会更新 Text 上打印的值,还记得只适用于 MutableState 吗?

Snippet above does not update the value printed on Text, does remember only work with MutableState?

推荐答案

remember - 允许您记住上次重构调用的状态,仅此而已.因此,例如,如果您在初始运行时随机化颜色.随机颜色将被计算一次,并在需要重新组合时重复使用.

remember - allows you to remember state from previous recompose invocation and just this. So if you for instance randomize color at initial run. The randomized color will going to be calculated once and reused whenever re-compose is necessary.

所以...记住 = 存储值以防调用 recompose.

so ... remember = store value just in case recompose will be called.

现在第二件事是知道何时应该真正触发重新编写.并且可变状态会有所帮助.

Now the second thing is knowing when re-compose should be actually triggered. and there mutable states comes to help.

mutablestate = 存储值 AND 以防我使用此数据更新所有元素的值触发重构.

mutablestate = store the value AND in case i update value trigger recompose for all elements using this data.

这篇关于Jetpack Compose 记得实际上做了什么,它是如何在引擎盖下工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆