gpt4 book ai didi

Avoid unnecessary Jetpack compose recomposition(避免不必要的Jetpack Compose重新组合)

翻译 作者:bug小助手 更新时间:2023-10-26 22:25:32 25 4
gpt4 key购买 nike



So I have this structure.

所以我有这样的结构。


sealed class ViewState {
object Loading : ViewState()

data class Content(
val toolbarUiModel: MyToolbarUiModel? = null,
val listUiModel : ListUiModel? = null,
...
) : ViewState()
}

And in activity

在活动中


val uiState: ViewState by viewModel.viewState.collectAsState()

when (uiState) {
is Content -> {
Content(
uiState
)
}

is EmptyState -> ...
}

And Content looks like this :

内容如下所示:


Content(
val state : ViewState
) {
...
state.listUiModel?.let { uiModel ->
MyListComponent()
}
...
}

Now what happens is even if I make changes only in ListUiModel or any single Ui Model, the entire Content composable is getting recomposed, because in ViewModel, I am doing

现在发生的情况是,即使我只在ListUiModel或任何单个Ui Model中进行更改,整个可合成的内容也会被重新组合,因为在ViewModel中,我正在


fun updateListUiMode() {
_state = _state.copy(listUiModel = newModel) // This triggers recomposition of the entire screen. However, only ListComponent is expected to be recomposed.
}

Is my code structure wrong? Because I don’t want to recompose entire screen when I update only a single component. Am I missing anything?

我的代码结构是否错误?因为当我只更新单个组件时,我不想重新组合整个屏幕。我错过了什么吗?


Thank you for all the help!

谢谢你的帮助!


更多回答

You have passed the state as a parameter in Content Composable to it will Recompose Entire Content

您已将状态作为可合成内容中的参数传递给它,它将重新合成整个内容

Thanks for the answer. I did try passing individual UI models to Content composable but still I am getting same result. I'll try one more time though

谢谢你的回答。我确实尝试了将单独的UI模型传递给Content Composable,但仍然得到了相同的结果。不过,我会再试一次

@Kaustubh you best approach would be having individual states for every single thing then that would mean you recompose only the necessary composables

@Kaustubh你最好的方法是对每一件事都有单独的状态,这意味着你只重新组合必要的可组合组件

优秀答案推荐
更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com