We use computed properties to automatically calculate values based on other data. We use watch to run code when data changes, like reacting to changes with side effects.
Watch vs computed decision in Vue
import { ref, computed, watch } from 'vue'; const count = ref(0); // Computed property const double = computed(() => count.value * 2); // Watcher watch(count, (newVal, oldVal) => { console.log(`Count changed from ${oldVal} to ${newVal}`); });
computed returns a reactive value that updates automatically.
watch runs a function when the watched data changes, useful for side effects.
const message = ref('Hello'); const shout = computed(() => message.value.toUpperCase());
watch(message, (newVal, oldVal) => {
console.log(`Message changed from ${oldVal} to ${newVal}`);
});const count = ref(0); const doubleCount = computed(() => count.value * 2); watch(count, (newVal) => { if (newVal > 10) { alert('Count is greater than 10!'); } });
This Vue component shows a count and its double. Clicking the button increases count. The computed property updates doubleCount automatically. The watcher logs changes to count in the console.
<template>
<div>
<p>Count: {{ count }}</p>
<p>Double: {{ doubleCount }}</p>
<button @click="increment">Increment</button>
</div>
</template>
<script setup>
import { ref, computed, watch } from 'vue';
const count = ref(0);
const doubleCount = computed(() => count.value * 2);
watch(count, (newVal, oldVal) => {
console.log(`Count changed from ${oldVal} to ${newVal}`);
});
function increment() {
count.value++;
}
</script>Use computed when you want to derive and display data reactively.
Use watch when you want to perform side effects like API calls or logging after data changes.
Computed properties cache their results until dependencies change, making them efficient.
Computed is for reactive, cached values based on other data.
Watch is for running code when data changes, especially side effects.
Choose computed for display logic, watch for reacting to changes.
Practice
Solution
Step 1: Understand computed properties
Computed properties automatically update when their dependencies change and cache their results for efficiency.Step 2: Compare with watch
Watch is for running code on data changes, not for caching reactive values.Final Answer:
Computed property -> Option AQuick Check:
Reactive cached value = Computed property [OK]
- Using watch to compute and cache values
- Confusing methods with computed properties
- Thinking lifecycle hooks handle reactive caching
fullName in Vue 3 Composition API using computed?Solution
Step 1: Identify correct function for computed
Thecomputedfunction takes a getter function and returns a reactive, cached value.Step 2: Check other options
watchis for side effects,reactivecreates reactive objects,refcreates reactive primitives, none are for computed caching.Final Answer:
const fullName = computed(() => firstName + ' ' + lastName); -> Option CQuick Check:
Computed syntax = computed(() => ...) [OK]
- Using watch instead of computed for caching
- Confusing reactive and ref with computed
- Missing arrow function inside computed
count.value changes from 0 to 1?const count = ref(0);
watch(count, (newVal, oldVal) => {
console.log(`Count changed from ${oldVal} to ${newVal}`);
});
count.value = 1;Solution
Step 1: Understand watch behavior
The watch function runs its callback when the watched reactive value changes, passing new and old values.Step 2: Analyze the code
Whencount.valuechanges from 0 to 1, the callback logs the change with oldVal=0 and newVal=1.Final Answer:
Count changed from 0 to 1 -> Option DQuick Check:
Watch logs old and new values on change [OK]
- Mixing newVal and oldVal order
- Expecting watch to cache values like computed
- Not calling .value on ref when changing
const count = ref(0);
watch(() => count, (newVal, oldVal) => {
console.log(newVal);
});
count.value = 5;Solution
Step 1: Understand watch source function
The watch source function should return the reactive value to watch, not the ref object itself.Step 2: Identify the mistake
The function returnscount(a ref), but it should returncount.valueto watch the actual value.Final Answer:
The watch source function should return count.value instead of count -> Option BQuick Check:
Watch source must return reactive value, not ref object [OK]
- Returning ref instead of its value in watch source
- Confusing parameter order in watch callback
- Assuming watch works without reactive source
Solution
Step 1: Separate display logic and side effects
Computed properties are ideal for reactive display values like full name, caching the result.Step 2: Use watch for side effects
Updating document title is a side effect, so watch the computed full name to react to changes.Final Answer:
Use a computed property for full name and a watch on full name to update document title -> Option AQuick Check:
Computed for values, watch for side effects [OK]
- Trying to update document title inside computed (not allowed)
- Watching first and last names separately instead of computed
- Using methods instead of computed for reactive values
Ready to prove it?
Pass the test and this pattern moves unknown
