Getters let you create values that update automatically when your store data changes. They help keep your app data organized and easy to use.
Getters for computed store values in Vue
Introduction
Syntax
Vue
const store = createStore({ state() { return { count: 0 } }, getters: { doubleCount(state) { return state.count * 2 } } })
Getters receive the current state as their first argument.
They behave like computed properties and update automatically.
Examples
Vue
getters: {
squaredCount(state) {
return state.count ** 2
}
}Vue
getters: {
isPositive(state) {
return state.count > 0
}
}Vue
getters: {
fullName(state) {
return `${state.firstName} ${state.lastName}`
}
}Sample Program
This example creates a store with a count of 5. The getter doubleCount returns twice that value. When we log it, we see 10.
Vue
import { createStore } from 'vuex' const store = createStore({ state() { return { count: 5 } }, getters: { doubleCount(state) { return state.count * 2 } } }) console.log(store.getters.doubleCount)
Important Notes
Getters are cached and only recalculate when their dependencies change.
You can access getters in components or other parts of your app via the store.
Summary
Getters compute values based on store state.
They update automatically when state changes.
Use getters to keep your data logic clean and reusable.
Practice
1. What is the main purpose of getters in a Vue store?
easy
Solution
Step 1: Understand what getters do
Getters compute values derived from the store's state without changing the state itself.Step 2: Compare options with this role
Only To compute and return derived state based on the store's data describes computing and returning derived state, which matches getters' purpose.Final Answer:
To compute and return derived state based on the store's data -> Option AQuick Check:
Getters compute derived state = B [OK]
Hint: Getters compute data, mutations change data [OK]
Common Mistakes:
- Confusing getters with mutations that modify state
- Thinking getters handle events or components
- Assuming getters register components
2. Which of the following is the correct syntax to define a getter named
completedTasks in a Vue store?easy
Solution
Step 1: Review correct getter syntax
A getter is a function receivingstateand returning a value. Arrow function with implicit return is common.Step 2: Check each option
getters: { completedTasks: (state) => state.tasks.filter(t => t.done) } correctly uses an arrow function withstateparameter and returns filtered tasks. getters: { completedTasks() { return this.tasks.filter(t => t.done) } } incorrectly usesthiswhich is not available. getters: { completedTasks: function() { return state.tasks.filter(t => t.done) } } lacksstateparameter. getters: { completedTasks: (state) => { state.tasks.filter(t => t.done) } } misses return statement.Final Answer:
getters: { completedTasks: (state) => state.tasks.filter(t => t.done) } -> Option BQuick Check:
Getter syntax needs state param and return value = C [OK]
Hint: Getters get state as first param and return computed value [OK]
Common Mistakes:
- Using
thisinside getters instead ofstate - Forgetting to return a value from the getter
- Not including
stateas a parameter
3. Given the Vue store state:
and getter:
What will
state: { items: [ { id: 1, done: true }, { id: 2, done: false } ] }and getter:
getters: { doneCount: (state) => state.items.filter(i => i.done).length }What will
store.getters.doneCount return?medium
Solution
Step 1: Analyze the state array
There are two items: one withdone: trueand one withdone: false.Step 2: Evaluate the getter logic
The getter filters items wheredoneis true, so only one item matches. Then it returns the length, which is 1.Final Answer:
1 -> Option CQuick Check:
Count of done items = 1 [OK]
Hint: Count items with done true using filter and length [OK]
Common Mistakes:
- Counting all items instead of filtered ones
- Returning the filtered array instead of its length
- Assuming undefined because of missing return
4. Identify the error in this Vue store getter definition:
getters: { activeUsers: (state) => { state.users.filter(u => u.active) } }medium
Solution
Step 1: Check getter function body
The getter uses curly braces but does not return the filtered array explicitly.Step 2: Understand arrow function return rules
When using curly braces in arrow functions, you must usereturnto output a value. Without it, the function returns undefined.Final Answer:
Missing return statement inside the getter function -> Option DQuick Check:
Arrow function with braces needs explicit return = A [OK]
Hint: Arrow functions with braces need explicit return [OK]
Common Mistakes:
- Assuming implicit return with curly braces
- Confusing parameter names for getters
- Thinking filter can't be used on arrays
5. You want a getter that returns the total price of items in a cart, but only for items with quantity > 0.
Given state:
Which getter correctly computes this total?
Given state:
state: { cart: [ { price: 10, quantity: 2 }, { price: 5 }, { price: 3, quantity: 4 } ] }Which getter correctly computes this total?
hard
Solution
Step 1: Understand the requirement
Only items with quantity > 0 should be counted in total price calculation.Step 2: Analyze each option
totalPrice: (state) => state.cart.reduce((sum, item) => sum + item.price * item.quantity, 0) sums all items regardless of quantity. totalPrice: (state) => state.cart.filter(i => i.quantity > 0).reduce((sum, item) => sum + item.price * item.quantity, 0) filters items with quantity > 0 before summing, which is correct. totalPrice: (state) => state.cart.map(i => i.price * i.quantity).reduce((a, b) => a + b) sums all items including those with zero quantity. totalPrice: (state) => state.cart.reduce((sum, item) => sum + item.price, 0) sums only prices ignoring quantity.Final Answer:
totalPrice: (state) => state.cart.filter(i => i.quantity > 0).reduce((sum, item) => sum + item.price * item.quantity, 0) -> Option AQuick Check:
Filter then reduce for quantity > 0 total = A [OK]
Hint: Filter items with quantity > 0 before summing [OK]
Common Mistakes:
- Not filtering out items with zero quantity
- Summing prices without multiplying by quantity
- Using map without filtering first
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
