Introduction to Pinia and Installation

Learn what Pinia is, why it replaces Vuex, and how to install and initialize it in a Vue 3 project.

🎯 Introduction

By the end of this article, you will understand what Pinia is, the problems it solves, and why Vue recommends it for state management in Vue 3. You will also install and initialize Pinia and build a basic counter store.


🧱 What Is Pinia?

Pinia is Vue’s officially recommended state management library, designed for Vue 3 with support for Vue 2 as well.

Its purpose is simple: keep data shared by multiple components in one central place. Any component can read or update the same data without passing props or emitting events through multiple layers.

For example:

  • Component A updates the user’s avatar.
  • Components B and C both need to display that avatar.

Without state management, A must notify its parent, which then passes the update to B and C. With Pinia, all three components read and write the same userStore, keeping the code much simpler.


✨ Why Vue 3 Recommends Pinia

Pinia was created to address several pain points of using Vuex in Vue 3 projects:

Pain pointVuexPinia
Updating stateDefine a mutation, then commit itUpdate state directly or use an action
TypeScriptLimited type supportBuilt-in support with natural type inference
Splitting modulesRequires namespace configurationEach store is independent by default
Learning curveMore concepts (state/mutation/action/module)Fewer concepts (state/getter/action)
Pinia simplifies the Vuex approach. If you already know Vuex, you can learn Pinia quickly.

🚀 Installing Pinia

In a Vue 3 project created with Vite or Vue CLI, install Pinia with a single command:

npm install pinia

After installation, register it in main.js:

main.js
import { createApp } from 'vue';
import { createPinia } from 'pinia';
import App from './App.vue';

const app = createApp(App);

app.use(createPinia());
app.mount('#app');
Call createPinia() before app.mount(); otherwise, components will not be able to find the Pinia instance.

🛠 Your First Counter Store

Once Pinia is registered, you can create a store. Here is a minimal example; the following articles will explain each part in detail.

stores/counter.js
import { defineStore } from 'pinia';

export const useCounterStore = defineStore('counter', {
    state: () => ({
        count: 0,
    }),
    getters: {
        double() {
            return this.count * 2;
        },
    },
    actions: {
        add() {
            this.count++;
        },
    },
});

Use it in a component:

App.vue
<template>
    <p>Count: {{ counter.count }}</p>
    <p>Double: {{ counter.double }}</p>
    <button @click="counter.add">Add 1</button>
</template>

<script setup>
import { useCounterStore } from './stores/counter.js';

const counter = useCounterStore();
</script>
useCounterStore follows a naming convention: start store functions with use and end them with Store so they are easy to recognize.

❓ Questions and Answers

Q1: Can Pinia only be used with Vue 3?

It also works with Vue 2, but offers the best experience in Vue 3, where it is the officially recommended choice.

Q2: How are Pinia and Vuex related?

Both are state management solutions in the Vue ecosystem. Pinia can be considered Vuex’s successor, and the official documentation lists it as the default recommendation for Vue 3.

Q3: Why must the first argument to defineStore be unique?

It is the store’s global ID. Pinia uses it to distinguish stores; duplicate IDs can cause state conflicts.

Q4: Must store files go in a stores directory?

No, but this community convention makes projects easier to maintain. Any location works as long as the import path is correct.


🧪 Exercise

Create a useScoreStore by following these steps:

  1. Define the store in stores/score.js with the ID 'score'.
  2. Add a score state property with an initial value of 0.
  3. Add an add() action that increases score by 10.
  4. Display score in a component and call add() when a button is clicked.
stores/score.js
import { defineStore } from 'pinia';

// Write your code here
App.vue
<template>
    <p>Score: {{ score.score }}</p>
    <button @click="score.add">Add points</button>
</template>

<script setup>
// Import and create the store instance here
</script>

🎉 You have completed your introduction to Pinia! Next, we will explore what state, getters, and actions do and how to use them.