Introduction to Pinia and Installation
🎯 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 point | Vuex | Pinia |
|---|---|---|
| Updating state | Define a mutation, then commit it | Update state directly or use an action |
| TypeScript | Limited type support | Built-in support with natural type inference |
| Splitting modules | Requires namespace configuration | Each store is independent by default |
| Learning curve | More concepts (state/mutation/action/module) | Fewer concepts (state/getter/action) |
🚀 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:
import { createApp } from 'vue';
import { createPinia } from 'pinia';
import App from './App.vue';
const app = createApp(App);
app.use(createPinia());
app.mount('#app');
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.
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:
<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:
- Define the store in
stores/score.jswith the ID'score'. - Add a
scorestate property with an initial value of0. - Add an
add()action that increasesscoreby10. - Display
scorein a component and calladd()when a button is clicked.
import { defineStore } from 'pinia';
// Write your code here
<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.
