0
0
Vueframework~10 mins

Watch and watchEffect in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Watch and watchEffect
Start Component Setup
Define reactive state
Set up watch/watchEffect
Reactive state changes
watch/watchEffect callback runs
Update side effects or logs
Component updates if needed
End
This flow shows how Vue tracks reactive state changes and runs watch or watchEffect callbacks to react to those changes.
Execution Sample
Vue
import { ref, watch, watchEffect } from 'vue';
const count = ref(0);
watch(count, (newVal) => console.log('watch:', newVal));
watchEffect(() => console.log('watchEffect:', count.value));
count.value++;
count.value++;
This code sets up a reactive count and uses watch and watchEffect to log changes when count updates.
Execution Table
StepActioncount.valuewatch callbackwatchEffect callbackConsole Output
1Initialize count0NoRuns immediatelywatchEffect: 0
2Increment count.value to 11Runs with newVal=1Runswatch: 1 watchEffect: 1
3Increment count.value to 22Runs with newVal=2Runswatch: 2 watchEffect: 2
4No further changes2NoNo
💡 No more changes to count.value, so watch and watchEffect callbacks stop running.
Variable Tracker
VariableStartAfter 1After 2Final
count.value0122
Key Moments - 3 Insights
Why does watchEffect run immediately but watch does not?
watchEffect runs its callback right away to track dependencies, as shown in step 1 of execution_table, while watch waits for the watched value to change before running.
Does watchEffect run every time count.value changes?
Yes, watchEffect runs whenever any reactive dependency inside it changes, so it runs at steps 2 and 3 as count.value changes.
Why does watch callback receive newVal but watchEffect does not?
watch callback gets the new value as an argument because it watches a specific source, while watchEffect runs a function that accesses reactive values directly without arguments.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is logged by watchEffect at step 1?
AwatchEffect: 1
Bwatch: 0
CwatchEffect: 0
DNo output
💡 Hint
Check the Console Output column at step 1 in execution_table.
At which step does the watch callback first run?
AStep 2
BStep 1
CStep 3
DNever
💡 Hint
Look at the watch callback column and see when it first says 'Runs'.
If count.value was not incremented, how would the watch callback behave?
AIt would run immediately once
BIt would never run
CIt would run twice
DIt would run only on component mount
💡 Hint
Refer to the explanation that watch runs only when the watched value changes.
Concept Snapshot
Vue watch and watchEffect:
- watch tracks specific reactive sources and runs callback on change
- watchEffect runs immediately and whenever any reactive dependency changes
- watch callback receives new and old values
- watchEffect callback accesses reactive values directly
- Use watch for specific tracking, watchEffect for general reactive effects
Full Transcript
This visual execution shows how Vue's watch and watchEffect functions work. We start with a reactive count set to 0. watchEffect runs immediately and logs the current count. watch waits until count changes to run. When count increments to 1 and then 2, both watch and watchEffect run their callbacks and log the new values. watchEffect runs every time the reactive value changes, while watch runs only when the specific watched value changes. This helps understand when and how these two reactive watchers trigger in Vue.