Install BlaC, write a Cubit, use it from React, share it between components and load data. About ten minutes.
Installation
Terminal window
pnpmadd@blac/core@blac/react
Terminal window
npminstall@blac/core@blac/react
Terminal window
yarnadd@blac/core@blac/react
BlaC needs React 18 or 19. TypeScript is optional, but recommended: state and method types flow from your class into useBloc.
Recommended tsconfig.json
A standard strict React setup works. The @blac(...) decorator (for options like keepAlive and isolated) works as a legacy (experimentalDecorators) or a TC39 decorator. To skip decorators, call it as a function instead: blac({ keepAlive: true })(MyCubit), which needs no compiler flags.
{
"compilerOptions":{
"target":"ESNext",
"jsx":"react-jsx",
"strict":true,
"useDefineForClassFields":true,
"experimentalDecorators":true,// only if you use @blac(...) decorator syntax
},
}
Step 1: Define a Cubit
A Cubit is a class that holds state and has methods that change 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.
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.
When CounterControls calls increment, CounterDisplay re-renders with the new count. CounterControls never reads state, so it doesn’t re-render at all. No provider, context or prop drilling is involved.
Step 4: Add business logic
Rules belong in the class. The component stays a view.
// patch deep-merges, so we only mention the key we change.
this.patch({items:this.state.items.filter((_, i)=> i !== index)});
};
getisEmpty(){
returnthis.state.items.length ===0;
}
}
Notice the two write styles: addTodo uses update and lists both keys (replacing the whole state), while removeTodo uses patch and mentions onlyitems (merging into the rest). Both are correct — the difference is exactly the replace-vs-merge rule from Step 1.
Getters like isEmpty compute their value on every read, so they can’t drift from items. When a component reads todo.isEmpty during render, tracking records what the getter read (items.length), and the component re-renders when that changes. Dependency Tracking has the full rules.
Step 5: Fetch something
An async action is a method that awaits and updates state as it goes. Model the lifecycle as a status union so the view can render loading and error states, and guard with a request id so a slow response can’t overwrite a newer one.
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.
Allows manipulation and formatting of text strings and determination and location of substrings within strings.
String(
function(local var)e: unknown
e)});
}
};
}
The view switches on state.status, and TypeScript narrows each branch. For real apps, @blac/async packages this pattern: a status slot in state, the latest request wins, and requests abort when the bloc is released. The Async guide covers cancellation and why BlaC doesn’t use Suspense.
What just happened?
When you call useBloc(CounterCubit):
The registry looks for an existing CounterCubit instance, and creates one if there is none.
It increments the instance’s ref count: how many components are using it.
During render, state is a proxy that records which values the component reads.
When state changes, the component re-renders only if one of the values it read changed.
On unmount, the ref count goes down. At zero, the instance is disposed.
The Mental Model explains why each step works this way: why a proxy instead of selectors, why disposal is automatic, why updates batch on a microtask.