As I understand when some composable "X" uses `val...
# compose
j
As I understand when some composable "X" uses
value
on some
State<T>
"S", then the Compose runtime automatically recomposes X when S changes. Potentially bypassing recomposing any composables that just forward S to X without calling
value
. In my app, I have a "container composables" that are responsible for observing streams from a view model and passing the observed values to children composables. In one of the containers I have
StateFlow<List<T>>.collectAsState()
and I pass
State<List<T>>
to children of the container. What should I do, if I want to pass a boolean indicating whether
List<T>
is empty? Can I somehow create a new
State<Boolean>
? And in general, should I always prefer to pass
State<T>
to composables instead of just
T
?
I guess something like this would work:
Copy code
val items = viewModel.items.collectAsState()

val isEmpty = items.value.isEmpty()
val isEmptyState: State<Boolean> = remember(isEmpty) {
    mutableStateOf(isEmpty)
}

Child1(items)
Child2(isEmptyState)
But it seems quite weird.
j
Don't over think it, just pass the boolean to Child2.
Copy code
val items = viewModel.items.collectAsState()
Child1(items)
Child2(items.value.isEmpty())
Reading from a
mutableStateOf
has it's own costs to manage subscriptions that are likely to far exceed the costs of just passing the boolean, which will be memoized when the value is unchanged.
👍 1