📖 Scenario: You are building a simple Vue 3 app that tracks a user's favorite color using a store. The store holds the color value and lets components read and update it.
🎯 Goal: Create a Vue 3 component that uses a store to display and update the favorite color. The component will show the current color and have a button to change it.
📋 What You'll Learn
Create a Pinia store with a state variable favoriteColor set to 'blue'
Add a function in the store called changeColor that sets favoriteColor to 'red'
Create a Vue component that imports and uses the store
Display the current favoriteColor in the component
Add a button in the component that calls changeColor when clicked
💡 Why This Matters
🌍 Real World
Stores help manage shared data in Vue apps, like user preferences or app settings, so components stay in sync easily.
💼 Career
Understanding how to use Pinia stores is important for Vue developers to build scalable and maintainable applications.
Progress0 / 4 steps
1
Create the Pinia store with initial state
Create a Pinia store named useColorStore using defineStore. Inside, define a state with a variable favoriteColor set to the string 'blue'.
Vue
Hint
Use defineStore from Pinia and return an object with favoriteColor set to 'blue' inside state.
2
Add a function to change the color in the store
Inside the useColorStore Pinia store, add an actions section. Define a function called changeColor that sets this.favoriteColor to 'red'.
Vue
Hint
Inside actions, write a function changeColor that updates this.favoriteColor to 'red'.
3
Import and use the store in a Vue component
Create a Vue 3 component using <script setup>. Import useColorStore from the store file. Call useColorStore() and assign it to a constant named colorStore.
Vue
Hint
Use import { useColorStore } from './store' and then call useColorStore() inside <script setup>.
4
Display the color and add a button to change it
In the Vue component template, display the current color using {{ colorStore.favoriteColor }}. Add a button with the text Change Color that calls colorStore.changeColor when clicked.
Vue
Hint
Use mustache syntax to show colorStore.favoriteColor and add a button with @click="colorStore.changeColor".
Practice
(1/5)
1. What is the main purpose of using a store in a Vue component?
easy
A. To share and manage state across multiple components easily
B. To style components with CSS variables
C. To handle component lifecycle hooks
D. To define routes for navigation
Solution
Step 1: Understand what a store does
A store holds shared data that multiple components can access and update.
Step 2: Identify the purpose in Vue components
Stores help components share and manage state without passing props or events repeatedly.
Final Answer:
To share and manage state across multiple components easily -> Option A
Quick Check:
Store = Shared state management [OK]
Hint: Stores share data between components without props [OK]
Common Mistakes:
Confusing stores with styling or routing
Thinking stores handle lifecycle hooks
Assuming stores are only for single components
2. Which is the correct way to import and use a Pinia store inside a Vue component's setup() function?
easy
A. import store from 'pinia'
B. const store = createStore()
C. const store = useStore()
D. const store = new Store()
Solution
Step 1: Recall Pinia store usage
Pinia stores are accessed inside components by calling the store function, usually named useStore().
Step 2: Check the options
Only const store = useStore() correctly calls the store function inside setup().
Final Answer:
const store = useStore() -> Option C
Quick Check:
Use useStore() to access store [OK]
Hint: Call useStore() inside setup() to get the store [OK]
C. counter should be imported from 'pinia' instead
D. setup() should return a function
Solution
Step 1: Check how the store is used
The code assigns counter = useCounterStore without parentheses, so it stores the function itself, not the store instance.
Step 2: Understand the effect
Calling counter.increment() fails because counter is a function, not the store object.
Final Answer:
useCounterStore is not called as a function -> Option A
Quick Check:
Call useCounterStore() to get store instance [OK]
Hint: Always call useCounterStore() with () [OK]
Common Mistakes:
Forgetting parentheses when calling store function
Assuming increment() is invalid
Confusing import source for store
5. You want to create a Vue component that uses a Pinia store to track a list of tasks. You need to add a new task only if it is not empty and not already in the list. Which code snippet correctly uses the store inside setup() to achieve this?
hard
A.
const taskStore = useTaskStore
function addTask(task) {
if (taskStore.tasks.indexOf(task) === -1) {
taskStore.tasks.push(task)
}
}
B.
const taskStore = useTaskStore()
function addTask(task) {
if (task && !taskStore.tasks.includes(task)) {
taskStore.tasks.push(task)
}
}
C.
const taskStore = useTaskStore()
function addTask(task) {
if (task !== '') {
taskStore.tasks = [...taskStore.tasks, task]
}
}
D.
const taskStore = useTaskStore()
function addTask(task) {
taskStore.tasks.push(task)
}
Solution
Step 1: Check store usage and function call
const taskStore = useTaskStore()
function addTask(task) {
if (task && !taskStore.tasks.includes(task)) {
taskStore.tasks.push(task)
}
correctly calls useTaskStore() to get the store instance and defines addTask with proper checks.
Step 2: Verify conditions for adding task
It checks if task is not empty and not already in taskStore.tasks before pushing, preventing duplicates and empty entries.
Final Answer:
correctly uses the store and conditions to add tasks safely -> Option B
Quick Check:
Call store with () and check conditions before push [OK]
Hint: Call store with () and check duplicates before push [OK]
Common Mistakes:
Not calling useTaskStore() as a function
Not checking for empty or duplicate tasks
Replacing tasks array instead of pushing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.