Jesse Hill
06/11/2021, 7:42 PMcomposable
for a route was being recomposed whenever the state inside my HiltViewModel changed. After thinking through it a bit I think I know the answer but wanted some confirmation before updating the post. I’ll put what I think is going on in the thread.Jesse Hill
06/11/2021, 7:45 PMcomposable
is being recomposed is because when I put a mutableStateOf
value in the ViewModel that is basically the same thing as if I structured the composable
like this:
composable("route") {
var count by mutableStateOf(0)
HomeScreen(count, updateCount = { count++ })
}
@Composable
fun HomeScreen(
homeCount: Int,
updateCount: () -> Unit,
) {
Log.i("Home", "Home Screen")
Column(modifier = Modifier.fillMaxSize()) {
Spacer(modifier = Modifier.height(25.dp))
Button(onClick = { updateCount() }) {
Text("Increment count")
}
Spacer(modifier = Modifier.height(25.dp))
Text("Count is: $homeCount")
}
}
Am I right to assume that anytime there is a state value either directly in the composable (or in a ViewModel being injected into the composable) that the composable containing the value with recompose if the state value changes?Marko Novakovic
06/11/2021, 8:06 PMState
inside ViewModel
is just hoisting it (moving it step above) but State
is still State
and behaviour around it is still the sameJesse Hill
06/11/2021, 8:06 PMMarko Novakovic
06/11/2021, 8:07 PMMarko Novakovic
06/11/2021, 8:09 PMMarko Novakovic
06/11/2021, 8:13 PM