Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 38 ยท Getters and Actions

Stores Using Other Stores

Learn how a Pinia store can use another store in getters and actions, where to call the use function, how to avoid circular dependencies, and how to share logic safely.

Real apps have stores that depend on each other. The cart needs to know who the user is. The checkout needs the cart and the user. The notifications store is called by almost everyone. Pinia makes this simple: a store can call another store's use function and read its state, getters and actions.

The basic pattern

Import the other store and call it inside the getter or action:

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

export const useCartStore = defineStore("cart", {
  state: () => ({ items: [] }),
  getters: {
    shippingCost() {
      const user = useUserStore();
      return user.isMember ? 0 : 5;
    },
  },
  actions: {
    checkout() {
      const user = useUserStore();
      if (!user.isSignedIn) throw new Error("Please sign in first");
      this.items = [];
    },
  },
});

Calling useUserStore() returns the same store every time, so it is cheap. Because the call is inside the getter, the getter is reactive to the other store: when user.isMember changes, shippingCost recomputes.

Why not call useUserStore() at the top of the file? Because the use function needs an active pinia, and at import time none exists yet. Inside a getter or action it runs later, when the app and pinia are ready.

flowchart LR
    Cart["cart store"] -->|"uses"| User["user store"]
    Checkout["checkout store"] -->|"uses"| Cart
    Checkout -->|"uses"| User

A healthy dependency graph flows one way. Nothing points back to a store that depends on it.

Try it: a cart that reads the user store

Switch between a guest and a member. The shipping cost, the discount and the ability to check out all come from the user store.

Cart and user stores working together

Avoid circular dependencies

If cart uses user and user uses cart, you have a cycle. In actions that is often harmless, because the other store is only called when the action runs. The danger is a cycle that is evaluated while a store is being created: in a setup store, calling the other store at the top level makes each wait for the other. You get an infinite loop or an undefined value.

Rules that keep you safe:

  1. In options stores, call other stores inside getters and actions, never in state().
  2. In setup stores, do not read another store's state at the top level of the setup function if that store might read yours. Read it inside a computed or a function.
  3. If two stores need each other, extract the shared part into a third store, or pass the needed value as an argument.

For example, instead of user calling cart.clear() on sign-out, let the component (or a small action) call both:

JavaScript
function signOutEverywhere() {
  useUserStore().signOut();
  useCartStore().clear();
}

A tiny helper like this keeps both stores free of each other.

Using another store from a plain module

The same rule applies to utility files. Call useXStore() inside the function, not at the top of the module:

JavaScript
// api.js
import { useUserStore } from "./stores/user";

export async function authorizedFetch(url) {
  const user = useUserStore(); // looked up when the function runs
  return fetch(url, { headers: { Authorization: `Bearer ${user.token}` } });
}

If that function can run outside a component or before the app is installed, see Using stores outside components.

Sharing state without sharing stores

Sometimes two stores only need to agree on a value. Rather than make one read the other, give each one the value it needs:

JavaScript
// checkout store action
actions: {
  pay() {
    const total = useCartStore().total;
    const buyer = useUserStore().name;
    return charge(buyer, total);   // plain function, easy to test
  },
},

The checkout store depends on two stores, while the cart and user stores know nothing about checkout. This fan-in is the healthiest shape: many small, independent stores feeding one that coordinates.

Testing stores that use other stores

Because the dependency is looked up at call time, a test can create a pinia, set the other store's state first, and then call your action. The testing chapter shows how to do this with a fresh pinia per test and how to stub whole stores.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where should an options store call another store's use function?

    Calling it inside a getter or action runs it after pinia is active, and keeps the getter reactive to the other store.
  2. 2. What is a circular dependency between stores?

    When A needs B and B needs A during creation, neither can finish. Extract a third store or pass values in.
  3. 3. Which dependency shape is healthiest?

    Dependencies that flow one way are easy to understand and test.
  4. 4. Does calling useUserStore() twice create two stores?

    A store is created once per pinia and reused.

Summary

  • A store can use another by calling its use function inside a getter or action.
  • Never call it at module top level or inside state().
  • Getters that read other stores stay reactive to them.
  • Break cycles with a third store or by passing values as arguments.
  • Prefer one coordinating store that depends on several small independent ones.

Next: Setup Stores