Julius Marozas
09/08/2020, 8:14 PMvalue
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
?Julius Marozas
09/08/2020, 8:23 PMval 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.jim
09/08/2020, 8:26 PMjim
09/08/2020, 8:26 PMval items = viewModel.items.collectAsState()
Child1(items)
Child2(items.value.isEmpty())
jim
09/08/2020, 8:29 PMmutableStateOf
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.