Skip to content

@pinia/root / pinia / DefineStoreOptionsInPlugin

DefineStoreOptionsInPlugin<Id, S, G, A>

Available options when creating a pinia plugin.

Extends

Type Parameters

Id extends string

S extends StateTree

G

A

Properties

actions

ts
actions: A;

Extracted object of actions. Added by useStore() when the store is built using the setup API, otherwise uses the one passed to defineStore(). Defaults to an empty object if no actions are defined.


getters?

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

Optional object of getters.

Inherited from

ts
Omit.getters

state()?

ts
optional state: () => S;

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

Returns

S

Inherited from

ts
Omit.state

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)
  }
})

Inherited from

ts
Omit.hydrate

Released under the MIT License.