Discover how to make your Vue app smarter by choosing between watch and computed the right way!
Watch vs computed decision in Vue - When to Use Which
Imagine you have a form where you want to update some values based on user input. You try to manually check every change and update related parts yourself.
Manually tracking changes and updating values is tiring and easy to mess up. You might forget to update something or cause extra work that slows down your app.
Vue's computed properties automatically update when their dependencies change, and watch lets you react to changes with custom code, making your app smarter and simpler.
if (inputChanged) { updateValue(); }const result = computed(() => input.value * 2); watch(input, (newVal) => { doSomething(newVal); });You can easily keep your data in sync and run code when things change, without messy manual checks.
Think of a shopping cart: computed properties can update the total price automatically, while watch can trigger a save to the server when the cart changes.
Manual updates are error-prone and slow.
Computed properties auto-update based on dependencies.
Watch lets you run code when data 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
