CHAPTER 16
Beginner
State Management with Pinia
Updated: May 18, 2026
5 min read
# CHAPTER 16
State Management with Pinia
1. Chapter Introduction
When multiple components across an application need to share the same data, "prop drilling" becomes painful. Pinia solves this with a global reactive store — data declared once, accessible anywhere. Pinia is Vue 3's official state manager, replacing Vuex with a much simpler API.2. Learning Objectives
- Understand why global state management is needed.
- Install and set up Pinia.
- Create stores with state, getters, and actions.
- Use stores in components.
- Build a shopping cart with Pinia.
3. Why Pinia?
text
4. Installation
bash
main.js
5. Creating a Store
javascript
6. Using the Store in Components
vue
7. Common Mistakes
-
Destructuring store state without
storeToRefs:const { items } = cartbreaks reactivity. Useconst { items } = storeToRefs(cart).
-
Calling actions without
():@click="cart.addItem"(missing args). Should be@click="cart.addItem(product)".
8. MCQs
Question 1
Pinia store ID (first arg to defineStore)?
Question 2
Store state is like?
Question 3
Store getters are like?
Question 4
storeToRefs() is used to?
Question 5
Can store actions be async?
Question 6
Where is Pinia registered?
Question 7
Access a store's state directly?
Question 8
Reset store to initial state?
Question 9
Pinia vs Vuex?
Question 10
Multiple components using same store see?
9. Interview Questions
- Q: Why is Pinia preferred over Vuex in Vue 3?
-
Q: What is
storeToRefsand why is it needed?
10. Summary
Pinia provides global reactive state with a Composition API that feels exactly like components —ref() for state, computed() for getters, async functions for actions. The storeToRefs helper enables safe destructuring. Every component accessing the same store sees the same reactive state.