Discover how a simple formula can save you from endless manual updates and bugs!
Why Computed in Composition API in Vue? - Purpose & Use Cases
Imagine you have a list of items and you want to show the total price. Every time the list changes, you have to manually recalculate and update the total in your code.
Manually recalculating values is easy to forget and can cause bugs. If you miss updating the total, the displayed number becomes wrong and confuses users.
Computed properties automatically update when their dependencies change. You just declare the formula once, and Vue keeps the value correct without extra work.
let total = 0; function updateTotal(items) { total = 0; for (const item of items) { total += item.price; } }
const total = computed(() => items.value.reduce((sum, item) => sum + item.price, 0));You can write clear, reactive code that always stays in sync with your data, making your app reliable and easier to maintain.
In a shopping cart, computed properties keep the total price updated instantly as users add or remove products, without extra code to track changes.
Manual updates are error-prone and tedious.
Computed properties auto-update based on dependencies.
This leads to cleaner, more reliable reactive apps.
Practice
computed properties in Vue's Composition API?Solution
Step 1: Understand computed properties role
Computed properties are reactive and update automatically when their dependencies change.Step 2: Compare with other options
Methods run on demand, static data doesn't update, and computed doesn't replace data but complements it.Final Answer:
To create reactive values that update automatically when dependencies change -> Option DQuick Check:
Computed = reactive auto-update [OK]
- Confusing computed with methods
- Thinking computed stores static data
- Believing computed replaces data option
fullName inside setup()?Solution
Step 1: Recall computed syntax
Computed takes a function returning a value, usually an arrow function with parentheses and curly braces if needed.Step 2: Check each option
const fullName = computed(() => fullName + ' ' + lastName); uses arrow function with parentheses and returns expression correctly. const fullName = computed { return firstName + ' ' + lastName }; misses parentheses and arrow. const fullName = computed(function() { firstName + ' ' + lastName }); lacks return statement. const fullName = computed(() => { firstName + ' ' + lastName }); has curly braces but no return, so returns undefined.Final Answer:
const fullName = computed(() => firstName + ' ' + lastName); -> Option AQuick Check:
Correct arrow function syntax = const fullName = computed(() => firstName + ' ' + lastName); [OK]
- Omitting parentheses in arrow function
- Forgetting return statement with curly braces
- Using wrong function syntax inside computed
setup():
const count = ref(2); const double = computed(() => count.value * 2); count.value = 5; console.log(double.value);What will be logged to the console?
Solution
Step 1: Understand reactive refs and computed
Initially, count.value is 2, so double.value is 4. Then count.value is updated to 5.Step 2: Compute updated value
Computed property recalculates automatically: 5 * 2 = 10.Final Answer:
10 -> Option CQuick Check:
Updated count * 2 = 10 [OK]
- Thinking computed doesn't update after ref changes
- Confusing count value with computed value
- Ignoring .value when accessing refs
setup():
const firstName = ref('John');
const lastName = ref('Doe');
const fullName = computed(() => {
firstName + ' ' + lastName
});Solution
Step 1: Analyze computed function body
The function uses curly braces but does not return any value explicitly.Step 2: Understand arrow function behavior
With curly braces, arrow functions need an explicit return; otherwise, they return undefined.Final Answer:
Missing return statement inside computed function -> Option AQuick Check:
Curly braces need return [OK]
- Omitting return in arrow function with braces
- Thinking refs can't be used in computed
- Believing computed must be outside setup
statusMessage that returns "Active" if isActive is true, "Inactive" if false, and "Unknown" if isActive is null or undefined. Which code correctly implements this inside setup()?Solution
Step 1: Understand the conditions
We need to check three cases: true, false, and null/undefined (unknown).Step 2: Evaluate each option
const statusMessage = computed(() => isActive.value ? 'Active' : 'Inactive'); only handles true/false, ignoring null/undefined. const statusMessage = computed(() => isActive.value === true ? 'Active' : isActive.value === false ? 'Inactive' : 'Unknown'); uses nested ternary to cover all three cases correctly. const statusMessage = computed(() => { if (isActive.value) return 'Active'; else return 'Inactive'; }); misses the unknown case and uses if-else without else if. const statusMessage = computed(() => isActive ? 'Active' : 'Inactive'); checks isActive directly without .value and misses unknown case.Final Answer:
const statusMessage = computed(() => isActive.value === true ? 'Active' : isActive.value === false ? 'Inactive' : 'Unknown'); -> Option BQuick Check:
All cases handled with nested ternary [OK]
- Ignoring null or undefined cases
- Not using .value to access ref
- Using simple if-else missing else if
Ready to prove it?
Pass the test and this pattern moves unknown
