Stores help keep data shared and organized across different parts of your app. Using stores in components makes it easy to read and change this shared data.
You want multiple components to share the same data and update together.
You need to keep track of user login status across pages.
You want to manage a shopping cart that many components can add to or show.
You want to keep app settings consistent everywhere.
You want to avoid passing data through many layers of components.
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.