All skills
antfu avatar

/pinia

@d02c484 official
by Anthony Fuantfu/skills5.9k stars
335

Pinia official Vue state management library, type-safe and extensible. Use when defining stores, working with state/getters/actions, or implementing store patterns in Vue apps.

Use this Skill: https://skilld.dev/gh/antfu/skills/pinia

This session only. Nothing lands on disk.

referencesfeatures-plugins.md

≈919 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Plugins

Plugins extend all stores with custom properties, methods, or behavior.

Basic Plugin

import { createPinia } from 'pinia'

function SecretPiniaPlugin() {
  return { secret: 'the cake is a lie' }
}

const pinia = createPinia()
pinia.use(SecretPiniaPlugin)

// In any store
const store = useStore()
store.secret // 'the cake is a lie'

Plugin Context

Plugins receive a context object:

import { PiniaPluginContext } from 'pinia'

export function myPiniaPlugin(context: PiniaPluginContext) {
  context.pinia   // pinia instance
  context.app     // Vue app instance
  context.store   // store being augmented
  context.options // store definition options
}

Adding Properties

Return an object to add properties (tracked in devtools):

pinia.use(() => ({ hello: 'world' }))

Or set directly on store:

pinia.use(({ store }) => {
  store.hello = 'world'
  // For devtools visibility in dev mode
  if (process.env.NODE_ENV === 'development') {
    store._customProperties.add('hello')
  }
})

Adding State

Add to both store and store.$state for SSR/devtools:

import { toRef, ref } from 'vue'

pinia.use(({ store }) => {
  // guard against overriding an existing value (important for SSR)
  if (!Object.hasOwn(store.$state, 'hasError')) {
    const hasError = ref(false)
    store.$state.hasError = hasError
  }
  store.hasError = toRef(store.$state, 'hasError')
})

Adding External Properties

Wrap non-reactive objects with markRaw():

import { markRaw } from 'vue'
import { router } from './router'

pinia.use(({ store }) => {
  store.router = markRaw(router)
})

Custom Store Options

Define custom options consumed by plugins:

// Store definition
defineStore('search', {
  actions: {
    searchContacts() { /* ... */ },
  },
  debounce: {
    searchContacts: 300,
  },
})

// Plugin reads custom option
import debounce from 'lodash/debounce'

pinia.use(({ options, store }) => {
  if (options.debounce) {
    return Object.keys(options.debounce).reduce((acc, action) => {
      acc[action] = debounce(store[action], options.debounce[action])
      return acc
    }, {})
  }
})

For Setup Stores, pass options as third argument:

defineStore(
  'search',
  () => { /* ... */ },
  {
    debounce: { searchContacts: 300 },
  }
)

TypeScript Augmentation

Custom Properties

import 'pinia'
import type { Router } from 'vue-router'

declare module 'pinia' {
  export interface PiniaCustomProperties {
    router: Router
    hello: string
  }
}

Custom State

declare module 'pinia' {
  export interface PiniaCustomStateProperties<S> {
    hasError: boolean
  }
}

Custom Options

declare module 'pinia' {
  export interface DefineStoreOptionsBase<S, Store> {
    debounce?: Partial<Record<keyof StoreActions<Store>, number>>
  }
}

Subscribe in Plugins

pinia.use(({ store }) => {
  store.$subscribe(() => {
    // React to state changes
  })
  store.$onAction(() => {
    // React to actions
  })
})

Nuxt Plugin

Create a Nuxt plugin to add Pinia plugins:

// plugins/myPiniaPlugin.ts
import { PiniaPluginContext } from 'pinia'

function MyPiniaPlugin({ store }: PiniaPluginContext) {
  store.$subscribe((mutation) => {
    console.log(`[🍍 ${mutation.storeId}]: ${mutation.type}`)
  })
  return { creationTime: new Date() }
}

export default defineNuxtPlugin(({ $pinia }) => {
  $pinia.use(MyPiniaPlugin)
})
<!-- Source references: - https://pinia.vuejs.org/core-concepts/plugins.html -->

Source: SKILL.md on GitHub

No alerts3d5 checks · Risk SAFE
  • Gen Agent Trust Hub3d

    This skill is a safe and well-structured documentation reference for Pinia, the official Vue state management library. It provides accurate code snippets, follows security best practices (such as recommending XSS protection for SSR), and utilizes official packages from the Vue and Nuxt ecosystems.

  • Socket3d

    No alerts

  • Snyk3d

    Risk: LOW · No issues

  • Runlayer7mo

    1/11 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at d02c484. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 days ago.

Activeupdated 4 days ago
Other metadata
metadata
{
  "author": "Anthony Fu",
  "version": "2026.9.25",
  "source": "Generated from https://github.com/vuejs/pinia, scripts located at https://github.com/antfu/skills"
}

README badge

README badge for antfu/skills/pinia

Pinia is the official Vue state management library providing type-safe, extensible stores with support for both Options and Composition APIs. Use it for defining reactive state, getters, and actions in Vue applications, with built-in TypeScript support and devtools integration.

Generated from the current SKILL.md.

Does Pinia work with both Options API and Composition API?
Yes. Pinia supports both Options API and Composition API styles with first-class TypeScript support for both.
How do I preserve reactivity when destructuring state from a store?
Use `storeToRefs()` when destructuring state and getters to maintain reactivity. Actions can be destructured directly since they're bound to the store.
Can I use Pinia outside of Vue components?
Yes. Pinia stores work in navigation guards, plugins, middlewares, and other contexts outside components, but you should call stores inside functions rather than at module scope, especially for SSR.
How do I test components that use Pinia stores?
Use `@pinia/testing` for unit testing with built-in support for mocking and stubbing stores.
Does Pinia have Nuxt integration?
Yes. Pinia integrates with Nuxt with auto-imports, auto-setup, and SSR-specific best practices documented in the skill.

Generated from the current SKILL.md. These answers refresh after source changes.