Using Stores in Components
🎯 Introduction
By the end of this article, you will know how to use Pinia stores correctly in components. We will focus on why direct destructuring breaks reactivity and the problem storeToRefs solves.
🧱 Getting a Store Instance
Call the corresponding use function inside <script setup>:
<script setup>
import { useCounterStore } from './stores/counter.js';
const counter = useCounterStore();
</script>
useCounterStore() returns the same instance. Regardless of how many components call it, they all work with the same state.📖 Reading state and getters
Once you have the instance, access its properties using dot notation:
<template>
<p>Count: {{ counter.count }}</p>
<p>Double: {{ counter.double }}</p>
</template>
<script setup>
import { useCounterStore } from './stores/counter.js';
const counter = useCounterStore();
</script>
🛠 Calling actions
Call actions just like ordinary methods:
<template>
<button @click="counter.add">Add 1</button>
<button @click="counter.addN(5)">Add 5</button>
</template>
⚡ Why Direct Destructuring Breaks Reactivity
A common mistake is to write:
const counter = useCounterStore();
const { count, double } = counter; // ❌ This breaks reactivity
The destructured count and double contain only the values at that moment. If the store’s count later changes, the view will not update because the local count is no longer connected to the store.
reactive object: direct destructuring loses reactivity.The Correct Approach: storeToRefs
Pinia provides storeToRefs to destructure state and getters from a store while preserving reactivity:
<script setup>
import { storeToRefs } from 'pinia';
import { useCounterStore } from './stores/counter.js';
const counter = useCounterStore();
// ✅ Preserve reactivity
const { count, double } = storeToRefs(counter);
// Destructure actions directly
const { add, addN } = counter;
</script>
storeToRefs applies to state and getters. Actions are not reactive data, so you can destructure them directly.💡 Comparing Three Approaches
| Approach | Preserves reactivity | When to use it |
|---|---|---|
counter.count | ✅ | Recommended; the safest default |
const { count } = counter | ❌ | Avoid this approach |
const { count } = storeToRefs(counter) | ✅ | When destructuring multiple state properties |
counter.count is always a reliable choice.❓ Questions and Answers
Q1: Can I write useCounterStore().count directly in a template?
Yes, but repeatedly calling the use function is less clear. Assign the store to a variable in <script setup>, such as const counter = useCounterStore().
Q2: Does storeToRefs turn the entire store into refs?
No. It converts state and getters into refs, leaving actions unchanged.
Q3: Why do actions not need storeToRefs?
Actions are methods rather than reactive data. Calling one simply executes a function; there is no changing value to track.
Q4: Do I still need storeToRefs when destructuring just one state property?
Yes. Use storeToRefs whenever you destructure state or a getter.
🧪 Exercise
This component destructures the store directly and loses reactivity. Update it to use the correct approach:
<template>
<p>Count: {{ count }}</p>
<p>Double: {{ double }}</p>
<button @click="add">Add 1</button>
</template>
<script setup>
import { useCounterStore } from '../stores/counter.js';
const { count, double, add } = useCounterStore(); // Update this line
</script>
🎉 You now know how to use stores correctly in components! Next, we will focus on ways to update state.
