The Three Store Essentials: state, getters, and actions

Understand state, getters, and actions, and learn to define and use Pinia stores.

🎯 Introduction

By the end of this article, you will understand the responsibilities of a Pinia store’s three core parts: state, getters, and actions. You will be able to write a well-structured store with clearly defined responsibilities.


🧱 Basic Store Structure

A typical store looks like this:

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++;
        },
    },
});

Each part has a specific role:

  • state: stores data
  • getters: compute values from data
  • actions: update data or run business logic
Think of a store as a small warehouse: state holds the shelves, getters provide price labels and reports, and actions are the warehouse operators.

📦 Storing Data with state

state must be a function that returns an object. Pinia automatically makes that object reactive.

stores/user.js
export const useUserStore = defineStore('user', {
    state: () => ({
        name: 'Alex',
        age: 20,
    }),
});

Read the state in a component:

<template>
    <p>{{ user.name }}{{ user.age }} years old</p>
</template>

<script setup>
import { useUserStore } from './stores/user.js';

const user = useUserStore();
</script>
Always define state as a function returning an object. Using an object directly could cause multiple application instances to share data and interfere with each other’s state.

⚡ Computing Values with getters

Methods in getters compute values from state and cache their results. As long as their state dependencies remain unchanged, repeated access does not recompute the value.

stores/user.js
export const useUserStore = defineStore('user', {
    state: () => ({
        firstName: 'Alex',
        lastName: 'Smith',
    }),
    getters: {
        fullName() {
            return this.firstName + this.lastName;
        },
    },
});

Usage:

<template>
    <p>Full name: {{ user.fullName }}</p>
</template>
Use this.xxx to access state inside a getter. Getters can also access other getters, such as this.otherGetter.

🛠 Updating Data with actions

Define methods that update state in actions. They can perform simple updates or encapsulate more complex logic.

stores/counter.js
export const useCounterStore = defineStore('counter', {
    state: () => ({
        count: 0,
    }),
    actions: {
        add() {
            this.count++;
        },
        addN(n) {
            this.count += n;
        },
        reset() {
            this.count = 0;
        },
    },
});

Call the actions in a component:

<template>
    <p>{{ counter.count }}</p>
    <button @click="counter.add">Add 1</button>
    <button @click="counter.addN(5)">Add 5</button>
    <button @click="counter.reset">Reset</button>
</template>
Update state with this.count inside an action. There is no need to commit a mutation as you would in Vuex, which makes Pinia easier to work with.

💡 Choosing the Right Tool

ScenarioUseExamples
Storing raw datastatecount, userInfo
Computing values from datagettersdouble, fullName
Updating data or running logicactionsadd, login
Avoid putting all your logic in components or writing every update directly in templates. Let templates handle simple state access and rendering, and keep state updates in store actions where possible.

❓ Questions and Answers

Q1: Both getters and actions can access state. How do they differ?

Getters are read-only: they compute and return derived values. Actions update state. One computes; the other changes data.

Q2: Can a getter call an action?

This is not recommended. Getters should perform pure computations without side effects. Use actions for side effects.

Q3: Can an action access a getter?

Yes, using this.xxx.

Q4: Can state contain functions?

This is not recommended. Keep data in state and functions in actions.


🧪 Exercise

Complete the following useTodoStore with:

  1. A todos array whose items have id, text, and done properties.
  2. A finishedCount getter that returns the number of completed items.
  3. An addTodo(text) action that adds an item.
  4. A toggle(id) action that toggles the matching item’s done value.
stores/todo.js
import { defineStore } from 'pinia';

export const useTodoStore = defineStore('todo', {
    state: () => ({
        todos: [],
    }),
    getters: {
        // Implement finishedCount here
    },
    actions: {
        // Implement addTodo and toggle here
    },
});

🎉 You now understand the three store essentials! Next, we will learn how to use stores in components, especially how to avoid losing reactivity.