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.