when using a UIState at a ViewModel. for example ,...
# compose
u
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
There have been many discussions on this, e.g. here.
u
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), )
UiState keeps the data structure compact, and each variable is separated to reduce the scope of reorganization