Skip to main content

Defining Stores

defineStore() supports two styles — the same two Pinia offers, named here without Vue's own vocabulary:

  • Schema stores — an object literal with state, getters, and actions fields. The style used throughout the Quick Start.
  • Hooks stores — a function you write like a custom React hook: compose ref()/computed()/plain functions, then return whatever should be public.

Both converge on the same underlying engine (a schema store is built by converting its options into the same shape a hooks store returns — see Reactivity Model), so there's no functional difference between them beyond what's called out on each page below. Pick whichever reads better for a given store, or see Enforcing a Store Style if you'd rather every store in a project agree on one.

At a glance​

 Schema storeHooks store
ShapedefineStore({ id, state, getters, actions })defineStore(id, () => {...})
Statestate factory functionref()/reactive() values you return
Gettersgetters object, (state) => ... or method with thiscomputed() values you return
Actionsactions object, methods with thisplain functions you return
Private (untracked) local statenot possible — every state key is publica ref()/local variable you don't return stays fully private
$reset()works — re-runs state()throws — see Hooks Stores
Best fitmost stores — reads declaratively, matches the Quick Starta store whose setup logic is easier to write as sequential code

Using a store​

Either style produces the same thing: a store definition — a plain function, conventionally named useXStore, that resolves (creating it on first call) the actual store instance against a Nekuta instance.

  • In a functional component: useStore(useCounterStore) — see Quick Start.
  • In a class component: connectStore({ counter: useCounterStore }, MyComponent) — see Class Components.
  • Outside React entirely (tests, plugin code, SSR data-fetching): call it directly, either with an explicit instance (useCounterStore(nekuta)) or with none, in which case it resolves against whichever instance is currently active — see setActiveNekuta.

A store is a singleton per Nekuta instance — calling useCounterStore(nekuta) twice against the same instance returns the exact same store object both times.