I have a flow that I'm getting state from with `co...
# compose
e
I have a flow that I'm getting state from with
collectAsState
. When that state updates, it means that something else (that isn't directly observable or a state) updated as well, and I want to use that in a composable. What's the better approach (aside from refactoring to get away from this issue):
Copy code
val current by foo.updates.collectAsState()
val bar by remember(current) { foo.bar }
or
Copy code
val current by foo.updates.collectAsState()
val bar by remember {
  derivedStateOf {
    current // just to get the update
    foo.bar
  }
}
I'm using
remember(current)
in the first one because it doesn't seem like the whole scope recomposes when
current
updates; only the scopes where
current
is actually read.
i
Have you considered using
produceState
? https://developer.android.com/jetpack/compose/side-effects#producestate
Copy code
val bar by produceState(initialValue = foo.bar, foo) {
  foo.updates.collect {
    value = foo.bar
  }
}
If you look at the source code for collectAsState(), you can see it is just using
produceState
under the hood
e
Would that still be a good choice if I also use the value of
current
further down the composable?
i
No, I'd just use the first one then
thank you color 1