when using a UIState at a ViewModel. for example , a UiState like this:
Copy code
data class UiState(
val dark: Boolean = false,
val showLoading: Boolean = true,
val showVisualization: Boolean = false,
val showSetting: Boolean = false
)
the compose binding this UiState by mutableStateOf(UiState()). I have a question. when a val of Uistate changed, it lead to the Compose reComposition. but some subCompose of whole Compose also will ReComposition , actually the val don't changed. for example ComposeAll including ComposeA and ComposeB which binding UiState. ComposeA binding dark, ComposeB binding showLoading. when dark chagne, ComposeB also will ReCompositon with ComposeA . split UiState to alone State Val and using UiState which is best practice ?
a
Albert Chang
08/22/2024, 5:00 AM
There have been many discussions on this, e.g. here.
u
王欢
08/22/2024, 5:25 AM
Yes, it is a good answer. As a tradeoff solution. sometimes I using UiState like this : data class UiState(
val dark: Boolean = mutableStateOf(false),
val showLoading: Boolean = mutableStateOf(false),
val showVisualization: Boolean = mutableStateOf(false),
val showSetting: Boolean = mutableStateOf(false),
)
王欢
08/22/2024, 5:27 AM
UiState keeps the data structure compact, and each variable is separated to reduce the scope of reorganization