英文:
Should I store the state of a variable that depends upon another variable?
问题
以下是翻译好的部分:
是否可以保存依赖变量的状态?
我正在通过官方的Android课程学习Android开发。在其中一个项目中,我遇到了一行代码,它保存了一个依赖于另一个已保存状态的变量的状态。代码如下:
> 在这段代码中,我们已经存储了变量currentDessertIndex的状态。由于currentDessertPrice和currentDessertImageId的值取决于currentDessertIndex,无论何时重新组合发生,这两个变量都将被分配正确的值,即使我们不保存这些变量的状态也是如此。
@Composable
private fun DessertClickerApp(
desserts: List<Dessert>
) {
var revenue by rememberSaveable { mutableStateOf(0) }
var dessertsSold by rememberSaveable { mutableStateOf(0) }
val currentDessertIndex by rememberSaveable { mutableStateOf(0) }
var currentDessertPrice by rememberSaveable {
mutableStateOf(desserts[currentDessertIndex].price)
}
var currentDessertImageId by rememberSaveable {
mutableStateOf(desserts[currentDessertIndex].imageId)
}
...
}
> 这是我认为应该的正确方式。
@Composable
private fun DessertClickerApp(
desserts: List<Dessert>
) {
var revenue by rememberSaveable { mutableStateOf(0) }
var dessertsSold by rememberSaveable { mutableStateOf(0) }
val currentDessertIndex by rememberSaveable { mutableStateOf(0) }
var currentDessertPrice = desserts[currentDessertIndex].price
var currentDessertImageId = desserts[currentDessertIndex].imageId
...
}
那么,你有什么建议?我是正确的吗,还是会有什么问题?
我改变了状态存储的方式。现在我希望您告诉我我是正确的,还是会有任何问题?
英文:
Is it okay to save the state of a dependent variable?
I am learning Android development through the official Android course. In one of the projects, I came across a line of code that saves the state of a variable that depends on another variable whose state has already been saved. The code is as follows:
> In this code, we have already stored the state of the variable currentDessertIndex. Since the value of currentDessertPrice and currentDessertImageId depends upon the currentDessertIndex whenever the recomposition happens, the two variables will be assigned the correct value of even if we do not store the state of those.
@Composable
private fun DessertClickerApp(
desserts: List<Dessert>
) {
var revenue by rememberSaveable { mutableStateOf(0) }
var dessertsSold by rememberSaveable { mutableStateOf(0) }
val currentDessertIndex by rememberSaveable { mutableStateOf(0) }
var currentDessertPrice by rememberSaveable {
mutableStateOf(desserts[currentDessertIndex].price)
}
var currentDessertImageId by rememberSaveable {
mutableStateOf(desserts[currentDessertIndex].imageId)
}
...
}
> This is what I think should be the right way to do it.
@Composable
private fun DessertClickerApp(
desserts: List<Dessert>
) {
var revenue by rememberSaveable { mutableStateOf(0) }
var dessertsSold by rememberSaveable { mutableStateOf(0) }
val currentDessertIndex by rememberSaveable { mutableStateOf(0) }
var currentDessertPrice = desserts[currentDessertIndex].price
var currentDessertImageId = desserts[currentDessertIndex].imageId
...
}
So, what do you suggest? Am I correct or does this invites any isssue?
I changed the way the the state was stored. Now I expect to tell me if I am correct or does this invite any issue.
答案1
得分: 1
理想情况下,只有在它所依赖的状态发生变化时,您才希望依赖状态发生变化。
var currentDessertPrice = desserts[currentDessertIndex].price
var currentDessertImageId = desserts[currentDessertIndex].imageId
然而,在上述代码中,依赖状态将在每次合成时都被重新计算,这是不必要的。因此,您必须使用remember
来在重新组合之间记住这些值。
另外,我认为这可以成为使用 derivedStateOf 的一个良好用例。
英文:
Ideally, you want a dependent state to change only when the state it depends on changes.
var currentDessertPrice = desserts[currentDessertIndex].price
var currentDessertImageId = desserts[currentDessertIndex].imageId
However, in the above lines, the dependant states will be calculated every time the composable recomposes which is unnecessary. So, you have to use remember to remember the values between recomposition.
Also, I think that this can be a good use case of derivedStateOf.
答案2
得分: 0
是的,通常最好只保存独立变量的状态。
如果依赖变量复杂(不是在您的情况下),您也可以保存它们的状态,但在独立变量更改时必须小心更新它们。否则,您将遇到难以找到的错误,其中一些部分的可组合性不会更新。
英文:
Yes it's generally better to only save the state of independent variables.
If the dependent variables are complex (not in your case), you can save their states too but you'll have to be careful to update them when the independent variable changes. Otherwise you will encounter hard to find bugs where some parts of your composable doesn't update.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论