So I asked a question on SO <here> about why the ...
# compose
j
So I asked a question on SO here about why the
composable
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.
So I think that the reason the
composable
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:
Copy code
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?
m
yes. having
State
inside
ViewModel
is just hoisting it (moving it step above) but
State
is still
State
and behaviour around it is still the same
👍 1
j
Thanks!
m
no problem
recently here was a discussion about it