Pinia: The Intuitive Store for Vue

3.0.4 · active · verified Sun Apr 19

Pinia is an intuitive, type-safe, and flexible state management library specifically designed for Vue.js applications. It serves as a lightweight and robust alternative to Vuex, leveraging Vue 3's Composition API for a more modern and streamlined development experience. The current stable major version is v3.x, with the latest release being v3.0.4, indicating active maintenance and minor updates within the major release. Pinia distinguishes itself through its excellent TypeScript support, providing strong type inference out-of-the-box, automatic module generation (no need for nested modules), and a plugin system. Its API is designed to be straightforward and familiar to Vue developers, making state management less verbose and more declarative. Releases within a major version (like v3.0.x) typically include bug fixes and minor improvements, with major version bumps (v2 to v3) often aligning with Vue major version releases and dropping support for older ecosystems (e.g., Vue 2).

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up Pinia in a Vue 3 application, define a basic store with state, getters, and actions, and use it within a Vue component.

import { createApp } from 'vue';
import { createPinia, defineStore } from 'pinia';

const app = createApp({
  template: `
    <div>
      <h1>Counter: {{ counter }}</h1>
      <p>Double Counter: {{ doubleCounter }}</p>
      <button @click="increment">Increment</button>
      <button @click="reset">Reset</button>
    </div>
  `,
  setup() {
    const main = useMainStore();
    return {
      counter: main.counter,
      doubleCounter: main.doubleCounter,
      increment: main.increment,
      reset: main.reset
    };
  }
});

// Define a Pinia store
const useMainStore = defineStore('main', {
  state: () => ({
    counter: 0,
    name: 'Pinia Example'
  }),
  getters: {
    doubleCounter: (state) => state.counter * 2
  },
  actions: {
    increment() {
      this.counter++;
    },
    reset() {
      this.counter = 0;
    }
  }
});

const pinia = createPinia();
app.use(pinia);
app.mount('#app');

// To make it runnable in a simple environment, assume an #app div exists:
document.body.innerHTML = '<div id="app"></div>';

view raw JSON →