简体   繁体   English

android jetpack compose中的“remember”和“mutableState”有什么区别?

[英]What is the difference between "remember" and "mutableState" in android jetpack compose?

I'm new in jetpack compose and trying to understand the difference between remember and mutableStateOf我是jetpack compose的新手,并试图了解remembermutableStateOf之间的区别


In other words the deference between this line换句话说,这条线之间的尊重

val text = remember{ mutableStateOf("") }

and this和这个

val text = remember{ "" }

and this also这也是

val text = mutableStateOf("")

remember is a composable function that can be used to cache expensive operations. remember是一个可组合的 function 可用于缓存昂贵的操作。 You can think of it as a cache which is local to your composable.您可以将其视为可组合的本地缓存。

val state: Int = remember { 1 }

The state in the above code is immutable.上述代码中的state是不可变的。 If you want to change that state and also update the UI, you can use a MutableState .如果您想更改 state 并更新 UI,您可以使用MutableState Compose will observe any reads/writes the MutableState object and triggers a recomposition to update the UI. Compose将观察MutableState object 的任何读/写操作,并触发重组以更新 UI。

val state: MutableState<Int> = remember { mutableStateOf(1) }

Text(
   modifier = Modifier.clickable { state.value += 1 },
   text = "${state.value}",
 )

Another variant (added in alpha12 ) called rememberSaveable which is similar to remember , but the stored value can survive process death or configuration changes.另一个变体(在alpha12中添加)称为rememberSaveable ,它类似于remember ,但存储的值可以在进程死亡或配置更改后继续存在。

val state: MutableState<Int> = rememberSaveable { mutableStateOf(1) }

Note : You can also use property delegates as a syntactic sugar to unwrap the MutableState .注意:您还可以使用属性委托作为语法糖来解开MutableState

var state: Int by remember { mutableStateOf(1) }

Regarding the last part of your question:关于你问题的最后一部分:

 val text = mutableStateOf("")

If you are doing the above, you are just creating a MutableState object without remembering it.如果您正在执行上述操作,那么您只是创建了一个MutableState object 而没有记住它。

MutableState is an alternative to using LiveData or Flow . MutableState是使用LiveDataFlow的替代方法。 Compose does not observe any changes to this object by default and therefore no recomposition will happen.默认情况下, Compose不会观察到此 object 的任何更改,因此不会发生重组。 If you want the changes to be observed and the state to be cached use remember .如果您希望观察更改并缓存 state,请使用remember If you don't need the caching but only want to observe, you can use derivedStateOf .如果您不需要缓存而只想观察,则可以使用derivedStateOf Here is a sample of how to use it.这是如何使用它的示例

As i understand.我认为。

remember just cache result of computation to keep result instance between compositions . remember缓存计算结果保持组合之间的结果实例 Any object.任何 object。 And MutableState instance.MutableState实例。 And this is why it is useful.这就是它有用的原因。

val text = remember{ "" }

just cache empty string.只缓存空字符串。

val text = mutableStateOf("")

create MutableState and compose observe it value, but not cache MutableState instance, so it will be re-created on next recomposition (of course if recomposition will happen in this place)创建MutableStatecompose观察它的值,但不缓存MutableState实例,所以它会在下一次重组时重新创建(当然如果重组会发生在这个地方)

for example:例如:

val state: MutableState<Int> =  mutableStateOf(1)
println(state.toString())
Text(
    modifier = Modifier.clickable { state.value += 1 },
    text = "${state.value}",
)

the text will always be 1, because every recomposition re-creates state and the output will be:文本将始终为 1,因为每次重组都会重新创建state并且 output 将是:

MutableState(value=1)@227069120
MutableState(value=1)@104526071
MutableState(value=1)@915621104
MutableState(value=1)@580489706 

remember caches MutableState object and keep same instance on every recomposition remember缓存MutableState object 并在每次重组时保持相同的实例

val state: MutableState<Int> = remember { mutableStateOf(1) }
println(state.toString())
Text(
    modifier = Modifier.clickable { state.value += 1 },
    text = "${state.value}",
)

work as expected.按预期工作。

MutableState(value=2)@1121832406
MutableState(value=3)@1121832406
MutableState(value=4)@1121832406
MutableState(value=5)@1121832406

remember(key)记住(键)

val key = remember { 0 }
var state by remember(key) { mutableStateOf(1) }
println(state.toString())
Text(
    modifier = Modifier.clickable { state += 1 },
    text = "${state}",
)

Works like the example above, even though the key doesn't change.像上面的例子一样工作,即使key没有改变。 It's because in case of a MutableState , not the value is cached, but the instance of MutableState itself with the value field, which changes.这是因为在MutableState的情况下,缓存的不是值,而是MutableState本身的实例以及value字段,它会发生变化。

changing key value will recreate MutableState instance更改key将重新创建MutableState实例

If remember is used with a field it's value will persist across recompositions.如果remember与字段一起使用,它的值将在重新组合中保持不变。

If mutableState is used with a field, all the composables which are using that field will be recomposed whenever the field values changes.如果mutableState与字段一起使用,则每当字段值更改时,所有使用该字段的可组合项都将被重新组合。

mutableStateOf(): it's an observable that observes the values when underlaying values gets changes and updates the UI. mutableStateOf():它是一个 observable,当底层值发生变化并更新 UI 时,它会观察这些值。 like we use liveData and stateFlows but LiveData also has lifecycle mechanism and almost same goes with StateFlows.就像我们使用liveDatastateFlows但 LiveData 也有生命周期机制,几乎和 StateFlows 一样。

remember{}: It persists the data across the recomposition.记住{}:它在整个重组过程中保留数据。
what is recomposition?什么是重组? When the state get changes, the composable that holds that value recomposes itself to give us updated value.当 state 发生变化时,保存该值的可组合对象会自行重组以提供更新的值。
for example: we have a textView(composable) that observes the value, right now which is 1 .例如:我们有一个 textView(composable) 观察值,现在它是1 We press the button and increment the value with 2 .我们按下按钮并用2增加值。 What happens here when the value goes from 1 to 2 the textView(composable) recreates( recomposes ) itself and shows us the updated value which is 2 , this is known as recomposition.当值从 1 变为 2 时,textView(composable) 会重新创建( recomposes )本身并向我们显示更新后的值2 ,这称为重组。

when we use this: val text = remember{ mutableStateOf("") } that's means we are not only observing the data but also persisting the data across the recomposition.当我们使用这个: val text = remember{ mutableStateOf("") }这意味着我们不仅在观察数据,而且还在整个重组过程中持久化数据。

Basically, in the first example you are storing a mutable value and in the second you are storing an immutable value.基本上,在第一个示例中,您存储了一个可变值,而在第二个示例中,您存储了一个不可变值。

According to the doc: "You can store immutable values when caching expensive UI operations, such as computing text formatting. The remembered value is stored in the Composition with the composable that called remember."根据文档:“在缓存昂贵的 UI 操作(例如计算文本格式)时,您可以存储不可变的值。记住的值与称为记住的可组合存储在组合中。” Source 资源

For more info on mutableStateOf, here is the doc link .有关 mutableStateOf 的更多信息,请参阅文档链接 You use this when you want your UI the be recomposed when there is a change in your values.当您希望在您的值发生变化时重新组合您的 UI 时,您可以使用它。

The remember keyword can store a mutable or an immutable object. remember 关键字可以存储一个可变或不可变的 object。 If you pass mutableStateOf to remember, any time the value of that object changes, it will force recomposition of the composables that are reading that value.如果你传递 mutableStateOf 来记住,只要 object 的值发生变化,它就会强制重新组合正在读取该值的可组合项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “= remember”和“by remember”有什么区别(Kotlin,Jetpack Compose) - What's difference between "= remember" and " by remember" (Kotlin, Jetpack Compose) Jetpack Compose 中记住和记住更新状态的区别? - Difference between remember and rememberUpdatedState in Jetpack Compose? Jetpack compose mutableState 和封装 - Jetpack compose mutableState and encapsulation 在 android 中创建自定义视图和使用 jetpack compose 有什么区别 - what is the difference between creating custom view and using jetpack compose in android 在 Jetpack Compose 中的分配和委派中使用记住有什么区别? - What's the difference between using remember in assignment vs in delegation in Jetpack Compose? Android MotionEvent 被 Jetpack Compose MutableState 更改/损坏 - Android MotionEvent gets changed/corrupted by Jetpack Compose MutableState Jetpack Compose:MutableState<boolean> 没有按预期工作</boolean> - Jetpack Compose: MutableState<Boolean> not working as intended Android Jetpack Compose:_uiState 和 uiState 有什么区别,我应该在 ViewModel 中阅读哪一个? - Android Jetpack Compose: what is the difference between _uiState and uiState and which one should I read in ViewModel? 使用jetpack compose时android studio gradle脚本中的compileSdk和compileSdkVersion有什么区别 - What is difference between compileSdk and compileSdkVersion in android studio gradle script when using jetpack compose 如何在 Jetpack Compose 的 MutableState 中使用条件 - How to use a condition in a MutableState in Jetpack Compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM