Why BlaC exists. Splitting UI, Logic and Data keeps components focused on rendering, logic testable without a DOM, and data sources replaceable.
Components tend to grow. One starts out rendering a button, and a few features later it holds useState, three useEffects, a fetch, validation rules and a loading flag, all before the return (.
BlaC is built on one premise: components shouldn’t own business logic, and business logic shouldn’t depend on the UI framework.
The problem with “fat components”
When state, logic and data fetching all live in components and custom hooks:
Components do too many jobs. Besides turning state into markup, they orchestrate async flows, guard against stale closures and compute derived flags.
Re-renders become your job.useMemo, useCallback and hand-written selectors spread through the code to keep unrelated views from re-rendering.
Testing needs a DOM. Checking a validation rule or a checkout flow means mounting a component tree, mocking providers and clicking simulated buttons.
The logic is tied to the view library. Reusing a feature in React Native or a script means untangling it from hooks first.
Three layers: UI, Logic and Data
BlaC splits an application into three layers, each with one job:
1. The UI Layer
Renders state and forwards interactions. Components turn state into
markup and call methods from onClick, onChange or onSubmit. They never
talk to the Data layer directly.
2. The Logic Layer (The Core)
Where the application lives.Cubit classes hold state, validate
input, derive values, coordinate features and call the Data layer. They
don’t depend on React or the DOM.
3. The Data Layer
Moves data in and out. REST, GraphQL, IndexedDB, localStorage or an
in-memory fake for tests. The Logic layer doesn’t care how data is stored,
and the UI doesn’t know this layer exists.
The one-way rule: UI never touches Data
The boundary that matters most: nothing travels between UI and Data without passing through the Logic layer. Components call methods; methods call the API; state flows back to the components that read it.
Cubit<S> is a StateContainer<S> that exposes mutation publicly.
That is the whole difference, and it is a real one: on StateContaineremit / patch / update are protected, so a container mutates itself
from its own methods and callers go through the API it chooses to publish.
Cubit re-declares the three as public for the cases where a caller
legitimately drives state from outside — test helpers, devtools
time-travel, benchmarks.
Reach for StateContainer when business logic should live in the class,
and Cubit when the caller owns the transitions.
React hook that connects a component to a state container with automatic
re-render on state changes.
Two tracking modes:
Auto-tracking (default): the returned state value is a proxy that
records read paths during render. The component re-renders when any
recorded path changes.
Manual select: pass options.select to opt out of auto-tracking.
The hook re-renders only when the returned array's elements change
(per-index Object.is).
Lifecycle:
The bloc is acquired from the registry on mount and released at the end
of the tick in which the component unmounts, so a consumer mounting in
the same commit takes over the instance instead of getting a new one.
The instance key is derived from options.args (own args), then the
surrounding
BlocProvider
context args for this bloc, then the
default key (no args).
If the bloc is disposed from outside while mounted (clear,
clearWhere, ...), the component re-renders against a new instance.
options.onMount fires after the bloc is acquired; options.onUnmount
fires before the registry releases its ref, so the bloc is still
alive when the callback runs.
Per-mount private instance: mark the class @blac({ isolated: true }), or
pass a unique key arg yourself (e.g. useId() in args). A per-mount
instance does not survive a first render that suspends: React discards that
render's hook state, so the retry gets a new instance. Do not use() a
promise owned by a per-mount bloc in the component that creates it.
@typeParam ― T - The state container constructor type (inferred from BlocClass)
@param ― BlocClass - The state container class to connect to
Cubit<S> is a StateContainer<S> that exposes mutation publicly.
That is the whole difference, and it is a real one: on StateContaineremit / patch / update are protected, so a container mutates itself
from its own methods and callers go through the API it chooses to publish.
Cubit re-declares the three as public for the cases where a caller
legitimately drives state from outside — test helpers, devtools
time-travel, benchmarks.
Reach for StateContainer when business logic should live in the class,
and Cubit when the caller owns the transitions.
Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
@param ― callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
@param ― initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
React hook that connects a component to a state container with automatic
re-render on state changes.
Two tracking modes:
Auto-tracking (default): the returned state value is a proxy that
records read paths during render. The component re-renders when any
recorded path changes.
Manual select: pass options.select to opt out of auto-tracking.
The hook re-renders only when the returned array's elements change
(per-index Object.is).
Lifecycle:
The bloc is acquired from the registry on mount and released at the end
of the tick in which the component unmounts, so a consumer mounting in
the same commit takes over the instance instead of getting a new one.
The instance key is derived from options.args (own args), then the
surrounding
BlocProvider
context args for this bloc, then the
default key (no args).
If the bloc is disposed from outside while mounted (clear,
clearWhere, ...), the component re-renders against a new instance.
options.onMount fires after the bloc is acquired; options.onUnmount
fires before the registry releases its ref, so the bloc is still
alive when the callback runs.
Per-mount private instance: mark the class @blac({ isolated: true }), or
pass a unique key arg yourself (e.g. useId() in args). A per-mount
instance does not survive a first render that suspends: React discards that
render's hook state, so the retry gets a new instance. Do not use() a
promise owned by a per-mount bloc in the component that creates it.
@typeParam ― T - The state container constructor type (inferred from BlocClass)
@param ― BlocClass - The state container class to connect to
It has no business logic. It doesn’t compute the total, decide whether submitting is allowed, or handle network errors.
It has no selectors. The render-time proxy records that it read checkout.total, checkout.canSubmit, state.status and state.orderId, and re-renders it when one of those changes.
It knows how to render the current state and which method to call. Nothing else.
Why this pays off
1. The UI can change
@blac/core has no React dependency, so a Cubit runs anywhere JavaScript does:
React and React Native:useBloc(CheckoutCubit), with the same class.
Other view libraries: a small adapter around watch() connects them, and the Logic and Data layers stay as they are.
2. The data source can change
The Logic layer calls plain functions or interfaces, like orderApi above. Storage that applies to many blocs, such as IndexedDB or localStorage, attaches from outside as a plugin:
Prototype against an in-memory fake.
Switch to REST or GraphQL when the backend is ready, by changing the API module.
When you do test a component, renderWithBloc mounts it with the state you choose, so you don’t mock fetch or replay user steps to reach that state.
Pragmatism over dogma
The three layers are a tool, not a rule:
Local UI state is fine. Whether a dropdown is open can stay in useState if nothing else cares.
No ceremony for small things. A feature doesn’t need interfaces, factories and three files. One Cubit next to the component already separates the logic and makes it testable.
Grow when it helps. Start with one Cubit. Later, move API calls into a data module, compose blocs with depend(), or use @blac/async for request cancellation and invalidation.