Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 38 ยท Getters and Actions

Getters

Learn Pinia getters: derive values from state, use this and other getters, pass arguments by returning a function, read other stores, and understand caching.

A getter is a value computed from a store's state. If state is the raw data, getters are the answers you derive from it: the total of a cart, the list of completed todos, the full name built from first and last name. In Pinia, getters are exactly Vue's computed properties, attached to the store.

Defining a getter

In an options store, getters are functions in the getters object. The simplest form takes the state as its first argument:

JavaScript
export const useCartStore = defineStore("cart", {
  state: () => ({
    items: [
      { name: "Pen", price: 2, qty: 3 },
      { name: "Book", price: 12, qty: 1 },
    ],
  }),
  getters: {
    count: (state) => state.items.length,
    total: (state) => state.items.reduce((sum, i) => sum + i.price * i.qty, 0),
  },
});

You read a getter like a property, without parentheses: cart.total. It is cached: Pinia recalculates it only when the state it read has changed, so using it in ten components costs one calculation.

Getters derived from the cart

Using this and other getters

A getter can read another getter through this. For that you must write it as a regular method (not an arrow function), because arrow functions have no this:

JavaScript
getters: {
  total: (state) => state.items.reduce((s, i) => s + i.price * i.qty, 0),
  totalWithTax() {
    return this.total * 1.2;
  },
},

this is the whole store, so it also gives access to state and actions. In TypeScript, a getter that uses this needs an explicit return type, as shown in Typing State with TypeScript.

An alternative that works with arrow functions is the second argument: (state, getters) => getters.total * 1.2. Both styles produce the same result; pick one and stay consistent.

Getters that take arguments

A getter itself cannot receive arguments, because it is a property. But it can return a function:

JavaScript
getters: {
  byName: (state) => (name) => state.items.find((i) => i.name === name),
},
JavaScript
cart.byName("Pen"); // { name: "Pen", price: 2, qty: 3 }

Note the trade-off: once a getter returns a function, Pinia caches the function, not the result of each call. Every call re-runs the lookup. That is fine for small collections; for large ones, store a lookup map (see below) or call a function from a component's computed.

A getter that returns a function

Getters that use other stores

A getter may call another store's use function. Do it inside the getter, not at the top of the file, so that the store is looked up when the getter runs:

JavaScript
import { useUserStore } from "./user";

export const useCartStore = defineStore("cart", {
  getters: {
    discount() {
      const user = useUserStore();
      return user.isMember ? 0.1 : 0;
    },
  },
});

The getter becomes reactive to the other store as well: when user.isMember changes, discount is recalculated. Stores using other stores explains this pattern and its limits.

Getters are read-only

You cannot assign to a getter. cart.total = 5 is ignored and warns in development. If you need a value that can be set, use state and an action to change it. Also avoid side effects in getters: no fetching, no changing state. A getter must return a value derived from what it reads, and nothing else.

A lookup map for large lists

Because argument getters do not cache per argument, a faster way to look up items by id in big lists is another getter that builds a map once:

JavaScript
getters: {
  byId: (state) => new Map(state.todos.map((t) => [t.id, t])),
},
JavaScript
todos.byId.get(2); // O(1) lookup, the map is rebuilt only when todos change

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How do you read a getter named total in a component?

    A getter is a property of the store; no parentheses and no getters object.
  2. 2. Why must a getter that uses this be written as a regular function?

    An arrow function does not bind its own this, so it cannot reach the store.
  3. 3. What is cached when a getter returns a function?

    Pinia caches the getter's value, which is the function. Each call to it runs again.
  4. 4. Which of these should a getter do?

    Getters are pure derived values. Side effects belong in actions.

Summary

  • Getters are cached computed values on the store, read as properties.
  • Use (state) => ... for simple ones and a regular method with this to reach other getters.
  • Return a function to accept arguments, remembering that the results are not cached.
  • Call other stores inside the getter body.
  • Getters are read-only and must be free of side effects.

Next: Actions