📖 Scenario: You are building a simple Vue 3 app that needs to share a counter value across components. To do this, you will create a Pinia store to hold and manage the counter state.
🎯 Goal: Build a Pinia store named useCounterStore that holds a count state starting at 0, a getter to get the double of count, and an action to increment count by 1.
📋 What You'll Learn
Create a Pinia store using defineStore.
Store must have a state with a count property initialized to 0.
Add a getter named doubleCount that returns count * 2.
Add an action named increment that increases count by 1.
💡 Why This Matters
🌍 Real World
Pinia stores help manage shared state in Vue apps, making it easy to keep data consistent across components.
💼 Career
Understanding Pinia is important for Vue developers to build scalable and maintainable applications.
Progress0 / 4 steps
1
Set up the Pinia store with initial state
Import defineStore from pinia and create a store named useCounterStore with a state function that returns an object with count set to 0.
Vue
Hint
Remember to import defineStore from pinia and define the store with a state function returning an object.
2
Add a getter to compute double the count
Inside the useCounterStore definition, add a getters object with a getter named doubleCount that returns state.count * 2. Use the state parameter in the getter function.
Vue
Hint
Getters are functions inside a getters object. Use state to access the store state.
3
Add an action to increment the count
Inside the useCounterStore definition, add an actions object with a method named increment that increases this.count by 1.
Vue
Hint
Actions are methods inside an actions object. Use this to access and modify state.
4
Export the complete Pinia store
Ensure the useCounterStore is exported as a named export using export const so it can be imported in Vue components.
Vue
Hint
Make sure the store is exported with export const so other files can use it.
Practice
(1/5)
1. What is the main purpose of creating a Pinia store in a Vue application?
easy
A. To handle routing between pages
B. To style components with CSS
C. To hold and manage shared state across components
D. To write HTML templates
Solution
Step 1: Understand what Pinia stores do
Pinia stores are designed to hold shared data (state) that multiple components can access and update.
Step 2: Identify the correct purpose
Styling, routing, and HTML templates are handled by other parts of Vue or related libraries, not Pinia stores.
Final Answer:
To hold and manage shared state across components -> Option C
Quick Check:
Pinia stores = shared state management [OK]
Hint: Pinia stores manage shared data, not styles or routes [OK]
Common Mistakes:
Confusing Pinia with CSS or routing libraries
Thinking Pinia handles templates
Assuming Pinia is for styling
2. Which of the following is the correct way to define a Pinia store named 'user' using defineStore?
The store is correctly defined with state as a function returning an object, and actions modifying state properly.
Step 2: Identify missing parts
The code snippet does not show importing defineStore from 'pinia', which is required to use defineStore.
Final Answer:
Missing import of defineStore from 'pinia' -> Option A
Quick Check:
Always import defineStore before use [OK]
Hint: Always import defineStore from 'pinia' before defining stores [OK]
Common Mistakes:
Forgetting to import defineStore
Thinking state must be an object, not a function
Believing actions cannot modify state
5. You want to create a Pinia store that keeps track of a user's profile with a name and age, and includes a getter to check if the user is an adult (age >= 18). Which of the following is the correct way to define this store?