Question
Why are every composables recomposed?
Could you explain why both TextField are being recomposed when I enter text in one ?
Using Compose (composeBom = "2024.06.00"), Voyager 1.0.0, Kotlin 1.9.23:
class RootScreen : Screen {
@Composable
override fun Content() {
val model = getScreenModel<RootScreenModel>()
val uiState = model.uiState.collectAsState()
println(uiState.value)
Column {
TextField(value = uiState.value.textField1, onValueChange = { model.updateTextField1(it) })
TextField(value = uiState.value.textField2, onValueChange = { model.updateTextField2(it) })
}
}
}
class RootScreenModel: ScreenModel {
private val _uiState = MutableStateFlow(ScreenUiState("", ""))
val uiState: StateFlow<ScreenUiState> = _uiState.asStateFlow()
fun updateTextField1(value: String) {
_uiState.update {
it.copy(textField1 = value)
}
}
fun updateTextField2(value: String) {
_uiState.update {
it.copy(textField2 = value)
}
}
}
data class ScreenUiState(val textField1: String, val textField2: String)
I activated Layout Inspector for Android Studio and I can see that both TextField are being recomposed for every new letter. Is it possible to recompose only the one that changed ?
Thanks !