blac

    State management for React & TypeScript v2 beta

    Components render.
    Classes think.

    BlaC puts state and business logic in plain TypeScript classes. Components read what they need and call methods. A component re-renders when a value it read changes, and state changes it didn't read never reach it.

    pnpm add @blac/core @blac/react
    AppCubitlive
    count0
    user.name"Ada"
    theme"dark"
    cart.items[0]

    <Counter /> renders 1

    Clicked 0 times

    count

    <Avatar /> renders 1

    A Ada

    user.name

    <Toolbar /> renders 1

    Hi Ada · ☾ dark

    user.nametheme

    <Cart /> renders 1

    empty

    cart.items

    Real useBloc, real renders. Press a jack: only what reads it lights up.

    Quick look

    One class. One hook. That’s the whole loop.

    No store to configure, no provider around your app, no reducers or selectors. The class owns the state and the methods that change it. The component renders state and calls methods.

    Counter.tsx
    import { Cubit } from '@blac/core';
    import { useBloc } from '@blac/react';
    class CounterCubit extends Cubit<{ count: number }> {
    constructor() {
    super({ count: 0 });
    }
    increment = () => this.emit({ count: this.state.count + 1 });
    }
    function Counter() {
    const [state, counter] = useBloc(CounterCubit);
    return <button onClick={counter.increment}>{state.count}</button>;
    }
    Counter — live blac island
    0renders1

    How it works

    Four ideas behind it

    Each one links to its reference page. The chapters take them apart step by step, with BlaC running on the stage beside the text.

    1. 01

      Touch it, you’re subscribed

      Reading state.user.name during render subscribes the component to that value and nothing else. Change the email and it stays put. There are no selectors to write and no dependency arrays to keep in sync.

    2. 02

      Components render. Classes think.

      State, rules and async work live in a class with no React in it. The component reads state and calls methods. The class runs in a plain unit test, and the API behind it can change without touching the UI.

    3. 03

      First in, last out

      The first component that uses a bloc creates it, and the last one to unmount disposes it. Instances are shared by default. Key them with args, give each mount its own with isolated, or keep one around with keepAlive.

    4. 04

      Blocs that talk

      A getter can read another bloc’s state through .track(). Components that read the getter re-render when either bloc changes it, with no second useBloc call.

    Packages

    A small core. The rest is opt-in.

    Testing

    Test the logic without rendering anything.

    A bloc is a class: construct it, call methods, check its state. No DOM, no providers, no mocked hooks. For component tests, renderWithBloc mounts a component with the state you choose.

    Testing guide
    cart.test.ts
    import { it, expect } from 'vitest';
    import { CartCubit } from './CartCubit';
    it('totals the cart', () => {
    const cart = new CartCubit();
    cart.add({ id: 'jack', price: 12 });
    cart.add({ id: 'cable', price: 4 });
    expect(cart.total).toBe(16);
    });

    Plug in.

    Start with the first chapter, or go straight to the reference.