What you get
{ user: { name } }store.x = 1store.a.b[0].c, or whatever nested shape you wantWhat you don't have to use
useMemo / useCallback danceimport { tracked, useReactive, For } from "@supergrain/kernel/react";
const TodoList = tracked(() => {
const { todos } = useReactive({
todos: [
{ id: 1, text: "Ship it", done: false },
{ id: 2, text: "Sleep", done: true },
],
});
return (
<For each={todos}>
{(todo) => (
<li onClick={() => (todo.done = !todo.done)}>
{todo.done ? "✓" : "○"} {todo.text}
</li>
)}
</For>
);
});js-framework-benchmark · weighted geometric mean
Lower is better. Supergrain matches raw useState — and beats every other state management library on the chart.
Source: js-framework-benchmark by Stefan Krause. Weighted geometric mean across create, update, replace, swap, select, remove, and clear scenarios.
A fast, ergonomic reactive store for React.
pnpm add @supergrain/kernelThe React subpath (@supergrain/kernel/react) ships in the same package and requires react >= 18.2.
Supergrain has two APIs for state. Use useReactive for state that lives inside a single component. Use createStoreContext for state shared across your app.
useReactive For state scoped to a single component, useReactive returns a reactive proxy that lives for the component's lifetime. No Provider, no setup — mutate it like a plain object.
// [#DOC_TEST_LOCAL_STATE](../doc-tests/tests/readme-react.test.tsx)
import { tracked, useReactive } from "@supergrain/kernel/react";
const Counter = tracked(() => {
const state = useReactive({ count: 0 });
return <button onClick={() => state.count++}>Clicked {state.count} times</button>;
});Wrap the component in tracked() to get fine-grained re-renders: only the properties you read are tracked.
createStoreContext For state shared across components, call createStoreContext<T>() once at module scope and destructure { Provider, useStore }. The Provider takes an initial prop; it constructs a reactive store from that data exactly once per mount, so every SSR request, every test, and every React tree gets an isolated store by construction.
Step 1: Describe the shape and call the factory.
// [#DOC_TEST_QUICK_START](../doc-tests/tests/readme-react.test.tsx)
// store.ts
import { createStoreContext } from "@supergrain/kernel/react";
export interface Todo {
id: number;
text: string;
completed: boolean;
}
export interface AppState {
todos: Todo[];
selected: number | null;
}
export const { Provider, useStore } = createStoreContext<AppState>();Step 2: Mount the Provider at the root. Pass the initial data; the Provider wraps it in createReactive per-mount, so SSR and tests are isolated automatically.
// main.tsx
import { Provider } from "./store";
import { App } from "./App";
<Provider
initial={{
todos: [
{ id: 1, text: "Learn Supergrain", completed: false },
{ id: 2, text: "Build something", completed: false },
],
selected: null,
}}
>
<App />
</Provider>;Step 3: Read the store from any descendant via useStore().
// TodoItem.tsx
import { tracked, useComputed } from "@supergrain/kernel/react";
import { useStore, type Todo } from "./store";
export const TodoItem = tracked(({ todo }: { todo: Todo }) => {
const store = useStore();
const isSelected = useComputed(() => store.selected === todo.id);
return (
<li className={isSelected ? "selected" : ""}>
<input
type="checkbox"
checked={todo.completed}
onChange={() => (todo.completed = !todo.completed)}
/>
{todo.text}
</li>
);
});Step 4: Use useComputed for derived values, useSignalEffect for side effects.
// App.tsx
import { tracked, useComputed, useSignalEffect, For } from "@supergrain/kernel/react";
import { useStore } from "./store";
import { TodoItem } from "./TodoItem";
export const App = tracked(() => {
const store = useStore();
const remaining = useComputed(() => store.todos.filter((t) => !t.completed).length);
useSignalEffect(() => {
const count = store.todos.filter((t) => !t.completed).length;
document.title = `${count} items left`;
});
return (
<div>
<h1>Todos ({remaining})</h1>
<For each={store.todos}>{(todo) => <TodoItem key={todo.id} todo={todo} />}</For>
</div>
);
});Checking a todo re-renders only that TodoItem. Changing selection re-renders only the 2 affected items. The App component and other items don't re-render.
From @supergrain/kernel. Framework-agnostic primitives.
createReactive<T>(initial)
Returns a reactive proxy you can read from and mutate directly. Use it for standalone reactive state outside React, or pair it with the React helpers below to drive component renders.
computed(fn)
Returns a derived value that recomputes lazily when its dependencies change. Use for memoized derivations outside React (
useComputedis the React-aware variant).
effect(fn)
Runs
fnimmediately and re-runs it whenever its dependencies change. Returns a stop function. Use outside React; for components, preferuseSignalEffect.
batch(fn)
Coalesces signal writes inside
fninto a single notification. Throws iffnreturns a Promise (must be sync).
Side-effect primitives (
resource,defineResource,reactivePromise,reactiveTask,dispose) and themodifierDOM helper live in@supergrain/husk— a thin layer built on top of this package.
From @supergrain/kernel/react. React-specific hooks and components.
useReactive<T>(initial)
Per-component reactive state. Creates the proxy once on mount; the identity stays stable across renders. Use for state scoped to a single component — no Provider needed.
createStoreContext<T>()
Returns
{ Provider, useStore }bound to a fresh React Context. Call once at module scope, destructure, re-export. The Provider takes aninitial: Tprop and wraps it increateReactive()once per mount — SSR requests and tests are isolated by construction. Each factory call mints a distinct Context, so two sibling Providers coexist without collision.
tracked(Component)
Wraps a React component with per-component signal scoping. Only the signals read during render are tracked — when they change, only this component re-renders.
useComputed(() => expr, deps?)
Shorthand for
useMemo(() => computed(factory), deps). Re-evaluates when upstream signals change, but only triggers a re-render when the result changes — acting as a firewall. Thedepsarray works exactly likeuseMemo: when deps change, a new computed is created.
useSignalEffect(() => sideEffect)
Shorthand for
useEffect(() => effect(fn), []). Runs a signal-tracked side effect that re-runs when tracked signals change and cleans up on unmount. Does not cause the component to re-render.
<For each={array} parent={ref?}>{item => ...}</For>
Optimized list rendering. Tracks which items actually changed and only re-renders those. When a
parentref is provided, swaps use O(1) direct DOM moves instead of O(n) React reconciliation.
React hooks for side effects (
useResource,useReactivePromise,useReactiveTask,useModifier) live in@supergrain/husk/react.
See how Supergrain compares to useState, Zustand, Redux, and MobX in the comparison guide.
Signal-level performance with a proxy experience. No new mental model — if you know JavaScript objects, you know Supergrain.
const store = createReactive({ count: 0, user: { name: "Jane" } });
// Read like a plain object
console.log(store.count); // 0
console.log(store.user.name); // 'Jane'
// Write like a plain object
store.count = 5;
store.user.name = "Alice";set() wrappers or updater functionsArrays and objects work exactly how you'd expect. Push, splice, assign, delete — all tracked, all reactive.
const store = createReactive({
items: ["a", "b", "c"],
user: { name: "Jane", age: 30 },
});
// Arrays
store.items.push("d");
store.items.splice(1, 1);
store.items[0] = "x";
// Objects
store.user.name = "Alice";
delete store.user.age;Nested objects and arrays are reactive at any depth. No observable() calls, no ref() wrappers — the entire tree is tracked automatically.
const store = createReactive({
org: {
teams: [{ name: "Frontend", members: [{ name: "Alice", active: true }] }],
},
});
// Change a deeply nested property
store.org.teams[0].members[0].active = false;
// Only components reading `active` on that specific member re-renderFine-grained means what doesn't re-render matters most. When one property changes, only the components that actually read that property update.
const store = createReactive({ count: 0, theme: "light" });
// Only re-renders when `count` changes — not when `theme` changes
const Counter = tracked(() => <p>{store.count}</p>);
// Only re-renders when `theme` changes — not when `count` changes
const Theme = tracked(() => <p>{store.theme}</p>);tracked()Derived values that act as a firewall. useComputed re-evaluates when upstream signals change, but only triggers a re-render when the result changes.
const store = createReactive({
selected: 3,
todos: [
/* 1000 items */
],
});
const TodoItem = tracked(({ todo }) => {
// Only re-renders when this specific item's selection state flips
const isSelected = useComputed(() => store.selected === todo.id);
return <li className={isSelected ? "active" : ""}>{todo.text}</li>;
});false → they don't re-render when selection changestrue↔false) updateuseMemo(() => computed(factory), deps)Signal-tracked side effects that run outside the React render cycle. They re-run when tracked signals change, but never cause the component to re-render.
const App = tracked(() => {
const store = Store.useStore();
const remaining = useComputed(() => store.todos.filter((t) => !t.completed).length);
useSignalEffect(() => {
document.title = `${remaining} items left`;
});
return <TodoList />;
});useEffect(() => effect(fn), [])<For> renders lists with per-item tracking. Only items that actually changed re-render — not the entire list.
const App = tracked(() => {
const store = Store.useStore();
return (
<For each={store.todos} parent={tableRef}>
{(todo) => <TodoItem key={todo.id} todo={todo} />}
</For>
);
});parent ref enables O(1) direct DOM moves for swapsparent, falls back to standard React reconciliationWrites are synchronous — you can always read your own writes:
store.count = 5;
console.log(store.count); // 5 — immediately availableSingle mutations are always safe. When you need to make multiple mutations atomically, wrap them in batch(). Without batching, each write fires reactive effects immediately — a computed that reads both swapped positions would run mid-swap and see a duplicate:
// ❌ Without batching — computed sees [C, B, C] after first write
const tmp = store.data[0];
store.data[0] = store.data[2]; // effects fire — data is [C, B, C]
store.data[2] = tmp; // effects fire again — data is [C, B, A]Wrap multi-step mutations in batch() so effects fire once with the final state:
import { batch } from "@supergrain/kernel";
batch(() => {
const tmp = store.data[0];
store.data[0] = store.data[2];
store.data[2] = tmp;
}); // effects fire once — data is [C, B, A]Supergrain wraps your state in a JavaScript Proxy. Plain objects and arrays are wrapped recursively; reading a property creates a signal on demand and subscribes the currently-running effect to it. Writing a property notifies that signal's subscribers. There's no upfront analysis of your state shape — the reactive graph is built lazily as your code touches properties.
Signal propagation is handled by alien-signals, the same primitive used by Vue Vapor and a handful of other modern reactive runtimes. It gives Supergrain push-based updates with topological ordering and glitch-free computed chains, without any manual scheduling.
tracked() is the bridge into React. Each tracked component runs its render inside its own signal-tracking scope, so the only signals it subscribes to are the ones it actually read this render. When one of those signals changes, only that component re-renders — never its parent, never its siblings. This is the per-component signal scoping that makes fine-grained reactivity possible in React's top-down model.
For SSR, Provider runs its initializer once per mount, which means each request gets its own fresh store instance. Reactive reads work on the server (no DOM dependencies) and the proxy survives serialization-equivalent traversal, so you can read state during render without special handling. Tests are isolated for the same reason: each <Provider> creates an independent store.
React features that trip up many state libraries — concurrent rendering, Suspense boundaries, the zombie-child problem — work here because reads are synchronous and per-component. There's no shared subscription that gets stale between renders, no torn-state window between commit and effect, and no useSyncExternalStore snapshot that has to be diffed across renders.
Map / Set reactive?Supergrain only proxies plain objects (Object constructor) and arrays. Class instances, Map, Set, Date, RegExp, and other built-ins pass through unchanged — they won't trigger re-renders when mutated. Store plain JSON-like data in your store; keep class instances and collections outside it. We may add Map/Set support in a later release.
batch() with await?No. batch() callbacks must be synchronous. The underlying batchDepth counter is global, so awaiting inside a batch would (a) silently lump every other write happening anywhere in the app into your batch, and (b) leave the depth elevated forever if the awaited promise rejects. The wrapper throws if your callback returns a Promise to make this explicit.
tracked() interact with React.memo?tracked() automatically wraps your component in React.memo (shallow prop equality), so a tracked component skips re-renders when its parent re-renders with the same prop references. The usual memo gotchas apply — passing a fresh inline object, array, or closure as a prop will defeat the equality check and cause re-renders even when the underlying data is unchanged.
Path autocompletion and type checking work up to 5 levels of nesting. Beyond that, paths fall through to a permissive Record<string, unknown> type. This limit exists because TypeScript's conditional-type recursion gets very expensive past depth 5; raising it would significantly slow type-checking for downstream consumers.
See the Comparison Guide. Briefly: most signal libraries (Preact Signals, MobX, Jotai) require you to wrap individual values in signal/atom containers. Supergrain wraps a whole object tree in a Proxy, so you write plain store.user.name = "x" and reads/writes are tracked automatically. Internally we use alien-signals for propagation, and tracked() gives per-component subscription scoping — closer in spirit to Solid's reactive components than to React Compiler's auto-memoization.
Not currently, and not planned. Subscriptions are per-signal — reading a property subscribes you to that property's signal, and that's the granularity. To react to a deep change like a.b.c = 1, something has to read a.b.c (or a computed derived from it) inside the effect.
If you need to observe a whole subtree, the practical patterns are: derive what you actually care about with useComputed, or read each leaf you depend on inside an effect/useSignalEffect.
MIT