API Reference
This section provides detailed documentation for all Pinia APIs.
Core APIs
Store Definition
- defineStore - Define a new store
- Store Instance - Store instance methods and properties
Pinia Instance
- createPinia - Create a Pinia instance
- Pinia Instance - Pinia instance methods
Utilities
- storeToRefs - Extract refs from store
- mapStores - Map stores for Options API
- mapState - Map state for Options API
- mapActions - Map actions for Options API
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.