Bloc Selector
fun <B : StateEmitter<S>, S : Any, V : Any> BlocSelector(bloc: B, selector: (state: S) -> V, content: @Composable (value: V) -> Unit)
A composable that derives a value from a StateEmitter's state and rebuilds content only when that derived value changes.
BlocSelector is the most targeted rebuild primitive in the Bloc library. Where BlocBuilder rebuilds on every emission and BlocBuilder with buildWhen rebuilds on approved transitions, BlocSelector projects the state down to a single V via a selector closure and uses equals equality to suppress redundant rebuilds.
Overview
// Only recomposes when isLoading flips — card list updates are ignored
BlocSelector(
bloc = lorcanaBloc,
selector = { it.isLoading },
) { isLoading ->
if (isLoading) CircularProgressIndicator()
}Content copied to clipboard
Composing multiple selectors
Derive multiple fields at once with an Equatable data class:
data class PaginationStatus(
val isLoadingMore: Boolean,
val hasMorePages: Boolean,
val cardCount: Int,
)
BlocSelector(
bloc = lorcanaBloc,
selector = { state ->
PaginationStatus(
isLoadingMore = state.isLoadingMore,
hasMorePages = state.hasMorePages,
cardCount = state.cards.size,
)
},
) { status ->
PaginationFooter(status)
}Content copied to clipboard
When to use BlocSelector
| Need | Use |
|---|---|
| Full state, rebuild on every emission | BlocBuilder |
| Rebuild at discrete thresholds | BlocBuilder with buildWhen |
| Rebuild only when a derived value changes | BlocSelector |
Parameters
bloc
The StateEmitter to observe.
content
A composable receiving the derived value. The closure receives V, not the full state, preventing accidental subscriptions to other fields.
fun <B : StateEmitter<S>, S : Any, V : Any> BlocSelector(blocClass: KClass<B>, selector: (state: S) -> V, content: @Composable (value: V) -> Unit)
Overload that resolves the bloc from BlocRegistry by KClass.
BlocSelector(
blocClass = LorcanaBloc::class,
selector = { it.isLoading },
) { isLoading ->
if (isLoading) CircularProgressIndicator()
}Content copied to clipboard