Skip to content

@pinia/root / pinia / DefineStoreOptions

DefineStoreOptions<Id, S, G, A> ​

Options parameter of defineStore() for option stores. Can be extended to augment stores with the plugin API.

See ​

DefineStoreOptionsBase.

Extends ​

Type Parameters ​

Id ​

Id extends string

S ​

S extends StateTree

G ​

G

A ​

A

Properties ​

actions? ​

ts
optional actions: A & ThisType<A & UnwrapRef<S> & _StoreWithState<Id, S, G, A> & _StoreWithGetters_Readonly<G> & _StoreWithGetters_Writable<G> & PiniaCustomProperties<string, StateTree, _GettersTree<StateTree>, _ActionsTree>>;

Optional object of actions.


getters? ​

ts
optional getters: G & ThisType<UnwrapRef<S> & _StoreWithGetters_Readonly<G> & _StoreWithGetters_Writable<G> & PiniaCustomProperties<string, StateTree, _GettersTree<StateTree>, _ActionsTree>> & _GettersTree<S>;

Optional object of getters.


id ​

ts
id: Id;

Unique string key to identify the store across the application.


state()? ​

ts
optional state: () => S;

Function to create a fresh state. Must be an arrow function to ensure correct typings!

Returns ​

S

Methods ​

hydrate()? ​

ts
optional hydrate(storeState, initialState): void;

Allows hydrating the store during SSR when complex state (like client side only refs) are used in the store definition and copying the value from pinia.state isn't enough.

Parameters ​

storeState ​

UnwrapRef<S>

the current state in the store

initialState ​

UnwrapRef<S>

initialState

Returns ​

void

Example ​

If in your state, you use any customRefs, any computeds, or any refs that have a different value on Server and Client, you need to manually hydrate them. e.g., a custom ref that is stored in the local storage:

ts
const useStore = defineStore('main', {
  state: () => ({
    n: useLocalStorage('key', 0)
  }),
  hydrate(storeState, initialState) {
    // @ts-expect-error: https://github.com/microsoft/TypeScript/issues/43826
    storeState.n = useLocalStorage('key', 0)
  }
})

Released under the MIT License.