Skip to content

API Reference

This section provides detailed documentation for all Pinia APIs.

Core APIs

Store Definition

Pinia Instance

Utilities

Quick Reference

Basic Store

js
import { defineStore } from 'pinia'

export const useStore = defineStore('main', {
  state: () => ({
    count: 0
  }),
  
  getters: {
    doubleCount: (state) => state.count * 2
  },
  
  actions: {
    increment() {
      this.count++
    }
  }
})

Composition API Store

js
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useStore = defineStore('main', () => {
  const count = ref(0)
  const doubleCount = computed(() => count.value * 2)
  
  function increment() {
    count.value++
  }
  
  return { count, doubleCount, increment }
})

Using Stores

js
import { useStore } from '@/stores/main'
import { storeToRefs } from 'pinia'

// In setup()
const store = useStore()

// Destructure with reactivity
const { count, doubleCount } = storeToRefs(store)
const { increment } = store

Type Definitions

Store

ts
interface Store<Id, S, G, A> {
  $id: Id
  $state: S
  $patch(partialState: Partial<S>): void
  $patch(stateMutator: (state: S) => void): void
  $reset(): void
  $subscribe(callback: SubscriptionCallback<S>): () => void
  $onAction(callback: ActionSubscriptionCallback<A>): () => void
  $dispose(): void
}

DefineStoreOptions

ts
interface DefineStoreOptions<Id, S, G, A> {
  id?: Id
  state?: () => S
  getters?: G
  actions?: A
  hydrate?(storeState: S, initialState: S): void
}

Explore the detailed API documentation in the following sections.

Released under the MIT License.