API Documentation / pinia

Module: pinia

Enumerations

Interfaces

Type aliases

GenericStore

Ƭ GenericStore<Id, S, G, A>: StoreWithState<Id, S, G, A> & UnwrapRef<S> & StoreWithGetters<G> & A & PiniaCustomProperties<Id, S, G, A> & PiniaCustomStateProperties<S>

Generic and type-unsafe version of Store. Doesn't fail on access with strings, making it much easier to write generic functions that do not care about the kind of store that is passed.

deprecated Use StoreGeneric instead

Type parameters

NameType
Idextends string``string
Sextends StateTreeStateTree
GGettersTree<S>
AActionsTree

Defined in

packages/pinia/src/types.ts:504


StateTree

Ƭ StateTree: Record<string | number | symbol, any>

Generic state of a Store

Defined in

packages/pinia/src/types.ts:13


Store

Ƭ Store<Id, S, G, A>: StoreWithState<Id, S, G, A> & UnwrapRef<S> & StoreWithGetters<G> & ActionsTree extends A ? {} : A & PiniaCustomProperties<Id, S, G, A> & PiniaCustomStateProperties<S>

Store type to build a store

Type parameters

NameType
Idextends string``string
Sextends StateTree{}
G{}
A{}

Defined in

packages/pinia/src/types.ts:472


StoreActions

Ƭ StoreActions<SS>: SS extends Store<string, StateTree, GettersTree<StateTree>, infer A> ? A : _ExtractActionsFromSetupStore<SS>

Extract the actions of a store type. Works with both a Setup Store or an Options Store.

Type parameters

Name
SS

Defined in

packages/pinia/src/store.ts:692


StoreGeneric

Ƭ StoreGeneric: Store<string, StateTree, GettersTree<StateTree>, ActionsTree>

Generic and type-unsafe version of Store. Doesn't fail on access with strings, making it much easier to write generic functions that do not care about the kind of store that is passed.

Defined in

packages/pinia/src/types.ts:491


StoreGetters

Ƭ StoreGetters<SS>: SS extends Store<string, StateTree, infer G, ActionsTree> ? StoreWithGetters<G> : _ExtractGettersFromSetupStore<SS>

Extract the getters of a store type. Works with both a Setup Store or an Options Store.

Type parameters

Name
SS

Defined in

packages/pinia/src/store.ts:705


StoreOnActionListener

Ƭ StoreOnActionListener<Id, S, G, A>: (context: StoreOnActionListenerContext<Id, S, G, {} extends A ? ActionsTree : A>) => void

Type parameters

NameType
Idextends string
Sextends StateTree
GG
AA

Type declaration

▸ (context): void

Argument of store.$onAction()

Parameters
NameType
contextStoreOnActionListenerContext<Id, S, G, {} extends A ? ActionsTree : A>
Returns

void

Defined in

packages/pinia/src/types.ts:228


StoreOnActionListenerContext

Ƭ StoreOnActionListenerContext<Id, S, G, A>: ActionsTree extends A ? _StoreOnActionListenerContext<StoreGeneric, string, ActionsTree> : { [Name in keyof A]: Name extends string ? _StoreOnActionListenerContext<Store<Id, S, G, A>, Name, A> : never}[keyof A]

Context object passed to callbacks of store.$onAction(context => {}) TODO: should have only the Id, the Store and Actions to generate the proper object

Type parameters

NameType
Idextends string
Sextends StateTree
GG
AA

Defined in

packages/pinia/src/types.ts:212


StoreState

Ƭ StoreState<SS>: SS extends Store<string, infer S, GettersTree<StateTree>, ActionsTree> ? UnwrapRef<S> : _ExtractStateFromSetupStore<SS>

Extract the state of a store type. Works with both a Setup Store or an Options Store. Note this unwraps refs.

Type parameters

Name
SS

Defined in

packages/pinia/src/store.ts:718


SubscriptionCallback

Ƭ SubscriptionCallback<S>: (mutation: SubscriptionCallbackMutation<S>, state: UnwrapRef<S>) => void

Type parameters

Name
S

Type declaration

▸ (mutation, state): void

Callback of a subscription

Parameters
NameType
mutationSubscriptionCallbackMutation<S>
stateUnwrapRef<S>
Returns

void

Defined in

packages/pinia/src/types.ts:152


SubscriptionCallbackMutation

Ƭ SubscriptionCallbackMutation<S>: SubscriptionCallbackMutationDirect | SubscriptionCallbackMutationPatchObject<S> | SubscriptionCallbackMutationPatchFunction

Context object passed to a subscription callback.

Type parameters

Name
S

Defined in

packages/pinia/src/types.ts:142

Variables

PiniaPlugin

Const PiniaPlugin: PluginInstallFunction & { install?: PluginInstallFunction }

deprecated use PiniaVuePlugin instead.

Defined in

packages/pinia/src/vue2-plugin.ts:76


PiniaVuePlugin

Const PiniaVuePlugin: Plugin

Vue 2 Plugin that must be installed for pinia to work. Note you don't need this plugin if you are using Nuxt.js. Use the buildModule instead: https://pinia.esm.dev/ssr/nuxt.html.

example

import Vue from 'vue'
import { PiniaVuePlugin, createPinia } from 'pinia'

Vue.use(PiniaVuePlugin)
const pinia = createPinia()

new Vue({
  el: '#app',
  // ...
  pinia,
})

param

Defined in

packages/pinia/src/vue2-plugin.ts:28

Functions

acceptHMRUpdate

acceptHMRUpdate(initialUseStore, hot): (newModule: any) => any

Creates an accept function to pass to import.meta.hot in Vite applications.

example

const useUser = defineStore(...)
if (import.meta.hot) {
  import.meta.hot.accept(acceptHMRUpdate(useUser, import.meta.hot))
}

Parameters

NameTypeDescription
initialUseStoreStoreDefinitionreturn of the defineStore to hot update
hotanyimport.meta.hot

Returns

fn

▸ (newModule): any

Parameters
NameType
newModuleany
Returns

any

Defined in

packages/pinia/src/hmr.ts:73


createPinia

createPinia(): Pinia

Creates a Pinia instance to be used by the application

Returns

Pinia

Defined in

packages/pinia/src/createPinia.ts:15


defineStore

defineStore<Id, S, G, A>(id, options): StoreDefinition<Id, S, G, A>

Creates a useStore function that retrieves the store instance

Type parameters

NameType
Idextends string
Sextends StateTree{}
Gextends GettersTree<S>{}
A{}

Parameters

NameTypeDescription
idIdid of the store (must be unique)
optionsOmit<DefineStoreOptions<Id, S, G, A>, "id">options to define the store

Returns

StoreDefinition<Id, S, G, A>

Defined in

packages/pinia/src/store.ts:741

defineStore<Id, S, G, A>(options): StoreDefinition<Id, S, G, A>

Creates a useStore function that retrieves the store instance

Type parameters

NameType
Idextends string
Sextends StateTree{}
Gextends GettersTree<S>{}
A{}

Parameters

NameTypeDescription
optionsDefineStoreOptions<Id, S, G, A>options to define the store

Returns

StoreDefinition<Id, S, G, A>

Defined in

packages/pinia/src/store.ts:757

defineStore<Id, SS>(id, storeSetup, options?): StoreDefinition<Id, _ExtractStateFromSetupStore<SS>, _ExtractGettersFromSetupStore<SS>, _ExtractActionsFromSetupStore<SS>>

Creates a useStore function that retrieves the store instance

Type parameters

NameType
Idextends string
SSSS

Parameters

NameTypeDescription
idIdid of the store (must be unique)
storeSetup() => SSfunction that defines the store
options?DefineSetupStoreOptions<Id, _ExtractStateFromSetupStore<SS>, _ExtractGettersFromSetupStore<SS>, _ExtractActionsFromSetupStore<SS>>extra options

Returns

StoreDefinition<Id, _ExtractStateFromSetupStore<SS>, _ExtractGettersFromSetupStore<SS>, _ExtractActionsFromSetupStore<SS>>

Defined in

packages/pinia/src/store.ts:772


getActivePinia

Const getActivePinia(): undefined | Pinia

Get the currently active pinia if there is any.

Returns

undefined | Pinia

Defined in

packages/pinia/src/rootStore.ts:40


mapActions

mapActions<Id, S, G, A, KeyMapper>(useStore, keyMapper): _MapActionsObjectReturn<A, KeyMapper>

Allows directly using actions from your store without using the composition API (setup()) by generating an object to be spread in the methods field of a component. The values of the object are the actions while the keys are the names of the resulting methods.

example

export default {
  methods: {
    // other methods properties
    // useCounterStore has two actions named `increment` and `setCount`
    ...mapActions(useCounterStore, { moar: 'increment', setIt: 'setCount' })
  },

  created() {
    this.moar()
    this.setIt(2)
  }
}

Type parameters

NameType
Idextends string
Sextends StateTree
Gextends GettersTree<S>
AA
KeyMapperextends Record<string, keyof A>

Parameters

NameTypeDescription
useStoreStoreDefinition<Id, S, G, A>store to map from
keyMapperKeyMapperobject to define new names for the actions

Returns

_MapActionsObjectReturn<A, KeyMapper>

Defined in

packages/pinia/src/mapHelpers.ts:326

mapActions<Id, S, G, A>(useStore, keys): _MapActionsReturn<A>

Allows directly using actions from your store without using the composition API (setup()) by generating an object to be spread in the methods field of a component.

example

export default {
  methods: {
    // other methods properties
    ...mapActions(useCounterStore, ['increment', 'setCount'])
  },

  created() {
    this.increment()
    this.setCount(2) // pass arguments as usual
  }
}

Type parameters

NameType
Idextends string
Sextends StateTree
Gextends GettersTree<S>
AA

Parameters

NameTypeDescription
useStoreStoreDefinition<Id, S, G, A>store to map from
keyskeyof A[]array of action names to map

Returns

_MapActionsReturn<A>

Defined in

packages/pinia/src/mapHelpers.ts:359


mapGetters

Const mapGetters<Id, S, G, A, KeyMapper>(useStore, keyMapper): _MapStateObjectReturn<Id, S, G, A, KeyMapper>

Alias for mapState(). You should use mapState() instead.

deprecated use mapState() instead.

Type parameters

NameType
Idextends string
Sextends StateTree
Gextends GettersTree<S>
AA
KeyMapperextends Record<string, keyof S | keyof G | (store: Store<Id, S, G, A>) => any>

Parameters

NameType
useStoreStoreDefinition<Id, S, G, A>
keyMapperKeyMapper

Returns

_MapStateObjectReturn<Id, S, G, A, KeyMapper>

Defined in

packages/pinia/src/mapHelpers.ts:285

Const mapGetters<Id, S, G, A, Keys>(useStore, keys): _MapStateReturn<S, G, Keys>

Alias for mapState(). You should use mapState() instead.

deprecated use mapState() instead.

Type parameters

NameType
Idextends string
Sextends StateTree
Gextends GettersTree<S>
AA
Keysextends string | number | symbol

Parameters

NameType
useStoreStoreDefinition<Id, S, G, A>
keysreadonly Keys[]

Returns

_MapStateReturn<S, G, Keys>

Defined in

packages/pinia/src/mapHelpers.ts:285


mapState

mapState<Id, S, G, A, KeyMapper>(useStore, keyMapper): _MapStateObjectReturn<Id, S, G, A, KeyMapper>

Allows using state and getters from one store without using the composition API (setup()) by generating an object to be spread in the computed field of a component. The values of the object are the state properties/getters while the keys are the names of the resulting computed properties. Optionally, you can also pass a custom function that will receive the store as its first argument. Note that while it has access to the component instance via this, it won't be typed.

example

export default {
  computed: {
    // other computed properties
    // useCounterStore has a state property named `count` and a getter `double`
    ...mapState(useCounterStore, {
      n: 'count',
      triple: store => store.n * 3,
      // note we can't use an arrow function if we want to use `this`
      custom(store) {
        return this.someComponentValue + store.n
      },
      doubleN: 'double'
    })
  },

  created() {
    this.n // 2
    this.doubleN // 4
  }
}

Type parameters

NameType
Idextends string
Sextends StateTree
Gextends GettersTree<S>
AA
KeyMapperextends Record<string, keyof S | keyof G | (store: Store<Id, S, G, A>) => any>

Parameters

NameTypeDescription
useStoreStoreDefinition<Id, S, G, A>store to map from
keyMapperKeyMapperobject of state properties or getters

Returns

_MapStateObjectReturn<Id, S, G, A, KeyMapper>

Defined in

packages/pinia/src/mapHelpers.ts:194

mapState<Id, S, G, A, Keys>(useStore, keys): _MapStateReturn<S, G, Keys>

Allows using state and getters from one store without using the composition API (setup()) by generating an object to be spread in the computed field of a component.

example

export default {
  computed: {
    // other computed properties
    ...mapState(useCounterStore, ['count', 'double'])
  },

  created() {
    this.count // 2
    this.double // 4
  }
}

Type parameters

NameType
Idextends string
Sextends StateTree
Gextends GettersTree<S>
AA
Keysextends string | number | symbol

Parameters

NameTypeDescription
useStoreStoreDefinition<Id, S, G, A>store to map from
keysreadonly Keys[]array of state properties or getters

Returns

_MapStateReturn<S, G, Keys>

Defined in

packages/pinia/src/mapHelpers.ts:231


mapStores

mapStores<Stores>(...stores): _Spread<Stores>

Allows using stores without the composition API (setup()) by generating an object to be spread in the computed field of a component. It accepts a list of store definitions.

example

export default {
  computed: {
    // other computed properties
    ...mapStores(useUserStore, useCartStore)
  },

  created() {
    this.userStore // store with id "user"
    this.cartStore // store with id "cart"
  }
}

Type parameters

NameType
Storesextends any[]

Parameters

NameTypeDescription
...stores[...Stores]list of stores to map to an object

Returns

_Spread<Stores>

Defined in

packages/pinia/src/mapHelpers.ts:96


mapWritableState

mapWritableState<Id, S, G, A, KeyMapper>(useStore, keyMapper): _MapWritableStateObjectReturn<S, KeyMapper>

Same as mapState() but creates computed setters as well so the state can be modified. Differently from mapState(), only state properties can be added.

Type parameters

NameType
Idextends string
Sextends StateTree
Gextends GettersTree<S>
AA
KeyMapperextends Record<string, keyof S>

Parameters

NameTypeDescription
useStoreStoreDefinition<Id, S, G, A>store to map from
keyMapperKeyMapperobject of state properties

Returns

_MapWritableStateObjectReturn<S, KeyMapper>

Defined in

packages/pinia/src/mapHelpers.ts:440

mapWritableState<Id, S, G, A>(useStore, keys): _MapWritableStateReturn<S>

Allows using state and getters from one store without using the composition API (setup()) by generating an object to be spread in the computed field of a component.

Type parameters

NameType
Idextends string
Sextends StateTree
Gextends GettersTree<S>
AA

Parameters

NameTypeDescription
useStoreStoreDefinition<Id, S, G, A>store to map from
keyskeyof S[]array of state properties

Returns

_MapWritableStateReturn<S>

Defined in

packages/pinia/src/mapHelpers.ts:458


setActivePinia

Const setActivePinia(pinia): undefined | Pinia

Sets or unsets the active pinia. Used in SSR and internally when calling actions and getters

Parameters

NameTypeDescription
piniaundefined | PiniaPinia instance

Returns

undefined | Pinia

Defined in

packages/pinia/src/rootStore.ts:34


setMapStoreSuffix

setMapStoreSuffix(suffix): void

Changes the suffix added by mapStores(). Can be set to an empty string. Defaults to "Store". Make sure to extend the MapStoresCustomization interface if you need are using TypeScript.

Parameters

NameTypeDescription
suffixMapStoresCustomization extends Record<"suffix", string> ? MapStoresCustomization["suffix"] : stringnew suffix

Returns

void

Defined in

packages/pinia/src/mapHelpers.ts:66


storeToRefs

storeToRefs<SS>(store): ToRefs<StoreState<SS> & StoreGetters<SS> & PiniaCustomStateProperties<StoreState<SS>>>

Creates an object of references with all the state, getters, and plugin-added state properties of the store. Similar to toRefs() but specifically designed for Pinia stores so methods and non reactive properties are completely ignored.

Type parameters

NameType
SSextends StoreWithState<string, StateTree, GettersTree<StateTree>, ActionsTree, SS> & {} & StoreWithGetters<GettersTree<StateTree>> & PiniaCustomProperties<string, StateTree, GettersTree<StateTree>, ActionsTree, SS> & PiniaCustomStateProperties<StateTree, SS>

Parameters

NameTypeDescription
storeSSstore to extract the refs from

Returns

ToRefs<StoreState<SS> & StoreGetters<SS> & PiniaCustomStateProperties<StoreState<SS>>>

Defined in

packages/pinia/src/storeToRefs.ts:13