Vue3 Fundamentals · State Management with Pinia

Using a store

using a store in a component or composable

Using a store

A store is only useful once a component reads it. You call the store's use... function to get the store instance, then read and write it like any reactive object.

Call it inside setup — or <script setup> — not at module scope. Pinia must be installed and active before the store is created, and that happens when the component is set up:

import { storeToRefs } from 'pinia';
import { useCounterStore } from './stores/counter';

const store = useCounterStore();
const { count, double } = storeToRefs(store);
const { increment } = store;

increment();

Destructuring needs care. Pull count straight out of the store and you get a plain number that never updates. storeToRefs returns refs instead, so the template keeps tracking them. Actions are different: they are bound to the store, so you can destructure them directly.

A composable can call a store the same way. That is how you share store logic across components without repeating it.

You are about to answer a short quiz on using a store.

  1. You should call a store's `use...` function inside `setup` or `<script setup>`.
  2. To destructure state and getters without losing reactivity, use ___.
  3. Destructuring an action from a store breaks its binding to the store.
  4. A store is obtained by calling its `use...` function, which returns a ___ instance.
  5. A composable can call `useStore()` just like a component can.