Watch and watchEffect help you react to changes in data automatically. They run code when something you care about changes.
Watch and watchEffect in Vue
import { watch, watchEffect } from 'vue'; // watch syntax watch(source, callback, options?); // watchEffect syntax watchEffect(effectFunction, options?);
watch watches specific reactive sources and runs a callback when they change.
watchEffect runs a function immediately and re-runs it whenever any reactive data inside changes.
watch(() => count.value, (newVal, oldVal) => {
console.log(`Count changed from ${oldVal} to ${newVal}`);
});watch([() => firstName.value, () => lastName.value], ([newFirst, newLast], [oldFirst, oldLast]) => {
console.log(`Name changed from ${oldFirst} ${oldLast} to ${newFirst} ${newLast}`);
});count changes.watchEffect(() => {
console.log(`Count is now ${count.value}`);
});This Vue component shows a button and a count. When you click the button, the count increases. The watch function updates the message whenever the count changes.
<template>
<div>
<button @click="count++">Increment</button>
<p>Count: {{ count }}</p>
<p>Message: {{ message }}</p>
</div>
</template>
<script setup>
import { ref, watch } from 'vue';
const count = ref(0);
const message = ref('');
watch(count, (newVal, oldVal) => {
message.value = `Count changed from ${oldVal} to ${newVal}`;
});
</script>Use watch when you want to react to specific data changes with old and new values.
Use watchEffect for simpler cases where you want to run code immediately and track dependencies automatically.
Remember to clean up side effects if needed inside watch or watchEffect to avoid memory leaks.
watch tracks specific reactive data and runs a callback on changes.
watchEffect runs code immediately and re-runs when any reactive data inside changes.
Both help keep your app reactive and update automatically when data changes.
Practice
watch and watchEffect in Vue?Solution
Step 1: Understand
watchbehaviorwatchlistens to specific reactive data sources you specify and runs a callback only when those change.Step 2: Understand
watchEffectbehaviorwatchEffectruns immediately and tracks all reactive data accessed inside it, rerunning whenever any of those change.Final Answer:
watchtracks specific reactive sources,watchEffecttracks all reactive dependencies automatically. -> Option BQuick Check:
watch vs watchEffect difference =watchtracks specific reactive sources,watchEffecttracks all reactive dependencies automatically. [OK]
- Thinking watchEffect only runs once
- Assuming watch runs immediately on setup
- Confusing cleanup requirements
count in Vue 3 Composition API?Solution
Step 1: Identify correct watch syntax
In Vue 3 Composition API,watchaccepts a ref directly as the first argument, which automatically watches its.value.Step 2: Check callback parameters
The callback receivesnewValandoldValas parameters, correctly used in watch(count, (newVal, oldVal) => { console.log(newVal) }) to log the new value.Final Answer:
watch(count, (newVal, oldVal) => { console.log(newVal) }) -> Option AQuick Check:
Correct watch syntax uses ref directly = watch(count, (newVal, oldVal) => { console.log(newVal) }) [OK]
- Using getter returning ref object like () => count
- Confusing watch with watchEffect syntax
- Omitting callback parameters
count.value = 2 is executed?import { ref, watch } from 'vue'
const count = ref(1)
watch(() => count.value, (newVal, oldVal) => {
console.log(`Changed from ${oldVal} to ${newVal}`)
})
count.value = 2Solution
Step 1: Understand watch callback parameters
The watch callback receives old and new values of the watched reactive source.Step 2: Trace the change
Initially, count.value is 1. When changed to 2, the callback logs the change from 1 to 2.Final Answer:
Changed from 1 to 2 -> Option DQuick Check:
watch logs old and new values correctly = Changed from 1 to 2 [OK]
- Expecting no output on change
- Confusing initial value with undefined
- Misunderstanding callback parameters
watch? How to fix it?import { ref, watch } from 'vue'
const count = ref(0)
watch(count, (newVal) => {
console.log(newVal)
})Solution
Step 1: Identify watch argument type
In Vue 3,watch(count, callback)is valid; it automatically trackscount.valuechanges.Step 2: Callback parameters
The callback receivesnewValand optionaloldVal; omittingoldValis fine.Final Answer:
No error; code works fine -> Option AQuick Check:
watch accepts ref directly = No error; code works fine [OK]
- Thinking ref must be wrapped in getter
- Assuming oldVal always required
- Believing code has error
Solution
Step 1: Identify immediate reactive effect
watchEffectruns immediately and tracks all reactive dependencies automatically.Step 2: Understand stopping effect
watchEffectreturns a stop function that can be called to stop the effect manually.Final Answer:
UsewatchEffectand store the returned stop function to call later -> Option CQuick Check:
watchEffect runs immediately and can be stopped = UsewatchEffectand store the returned stop function to call later [OK]
- Thinking watchEffect cannot be stopped
- Using watch without immediate for immediate run
- Ignoring stop function usage
Ready to prove it?
Pass the test and this pattern moves unknown
