BlocBuilder

fun <B : StateEmitter<S>, S : Any> BlocBuilder(bloc: B, content: @Composable (state: S) -> Unit)

A composable that subscribes to a StateEmitter's state stream and passes the current state to content on every emission.

Use BlocBuilder as a concise way to bind bloc state to UI. To restrict rebuilds to specific transitions, use the buildWhen overload instead.

Usage

val bloc = remember { BlocRegistry.resolve(CounterBloc::class) }

BlocBuilder(bloc) { state ->
Text("Count: ${state.count}")
Button(onClick = { bloc.add(CounterEvent.Increment) }) { Text("+") }
}

Resolving from the registry

BlocBuilder(CounterBloc::class) { state ->
Text("Count: ${state.count}")
}

Parameters

bloc

The StateEmitter to observe.

content

A composable receiving the latest state snapshot on every emission.


fun <B : StateEmitter<S>, S : Any> BlocBuilder(blocClass: KClass<B>, content: @Composable (state: S) -> Unit)

Overload that resolves the bloc from BlocRegistry by KClass.

BlocBuilder(CounterBloc::class) { state -> ... }

fun <B : StateEmitter<S>, S : Any> BlocBuilder(bloc: B, buildWhen: (previous: S, current: S) -> Boolean, content: @Composable (state: S) -> Unit)

A composable that rebuilds content only when buildWhen approves a state transition.

Unlike the plain BlocBuilder, this overload passes a state snapshot to content and updates that snapshot only when buildWhen(previous, current) returns true. This prevents UI sections from recomposing on every emission when only a specific field or threshold matters.

Usage

// Tier badge only redraws when the score crosses a tier boundary
BlocBuilder(
bloc = scoreBloc,
buildWhen = { old, new -> old / 10 != new / 10 },
) { state ->
TierBadge(tier = tierFor(state))
}

The "previous" value passed to buildWhen is the last displayed state — what was last rendered — not the last emitted state. This mirrors the iOS BlocBuilderWhen behaviour.

Parameters

bloc

The StateEmitter to observe.

buildWhen

Predicate (previous, current) -> Boolean. Return true to approve a rebuild. The previous value is the last state that was approved and rendered.

content

A composable receiving the last approved state snapshot.


fun <B : StateEmitter<S>, S : Any> BlocBuilder(blocClass: KClass<B>, buildWhen: (previous: S, current: S) -> Boolean, content: @Composable (state: S) -> Unit)

Overload that resolves the bloc from BlocRegistry by KClass.