Skip to content

createStore

createStore<TState, TActions>(config): KitStore<TState, TActions>

Defined in: create-store.ts:164

Create a typed Zustand store with persistence, migrations, and devtools.

This is a thin factory over Zustand’s create(). It bakes in conventions: versioned persistence, migration chains, devtools wiring, and a reset() method that clears both state and storage. State is shallow-frozen in development to catch accidental top-level mutations early — nested mutations are not detected.

Failures during hydration, persistence, migration, or reset are reported via the optional onError hook. They never throw to the caller — losing persistence is preferable to crashing the app, but you almost certainly want to know when it happens.

TState extends object

Shape of the state slice

TActions extends object = Record<string, never>

Shape of the actions object

CreateStoreConfig<TState, TActions>

Store configuration

KitStore<TState, TActions>

A Zustand hook augmented with reset() and destroy()

const useCounter = createStore({
name: "counter",
initial: { count: 0 },
actions: (set) => ({
increment: () => set((s) => ({ count: s.count + 1 })),
decrement: () => set((s) => ({ count: s.count - 1 })),
}),
});
// In a component
const count = useCounter((s) => s.count);
const { increment } = useCounter.getState();
const useUser = createStore({
name: "user",
initial: { id: null as string | null, prefs: {} },
actions: (set) => ({
setUser: (id: string) => set({ id }),
}),
persist: {
storage: "local",
version: 2,
migrate: {
2: (old: any) => ({ ...old, prefs: old.preferences ?? {} }),
},
},
onError: (err, info) => console.warn(`[store:user:${info.op}]`, err),
});