createStore
Function: createStore()
Section titled “Function: 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.
Type Parameters
Section titled “Type Parameters”TState
Section titled “TState”TState extends object
Shape of the state slice
TActions
Section titled “TActions”TActions extends object = Record<string, never>
Shape of the actions object
Parameters
Section titled “Parameters”config
Section titled “config”CreateStoreConfig<TState, TActions>
Store configuration
Returns
Section titled “Returns”KitStore<TState, TActions>
A Zustand hook augmented with reset() and destroy()
Examples
Section titled “Examples”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 componentconst 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),});