Vue's watchEffect Fires Twice: It's Not a Bug, It's How Dependency Tracking Works
First look at this code:
const { stop } = watchEffect(() => {
if (route.query.type) {
console.log(route.query.type, 'route.query.type')
}
})
watchEffect executing twice is usually not an error. The most common reason is: it runs once immediately, then runs again when reactive data it depends on changes.
1. watchEffect runs immediately
When a watchEffect is created, it runs the passed callback right away to collect the reactive dependencies used inside the callback.
So this code runs once when the component executes:
watchEffect(() => {
console.log(route.query.type)
})
This is different from watch: watch does not run immediately by default unless you set immediate: true.
2. route.query.type is reactive data
Reading this inside the callback:
route.query.type
Vue tracks that reactive dependency. When the route changes and route.query is updated, watchEffect runs again.
For example, at page initialization the route does not yet have type, then the route completes its update:
First time: watchEffect runs immediately, `type` has no value, so nothing is printed
Second time: route updates, `type` now has a value, so it prints
It could also be that the first run already has a value and prints once, then later the query parameter changes again and prints a second time.
3. Reads inside an if are also tracked
Even though route.query.type is written inside an if, the condition itself reads it:
if (route.query.type) {
// ...
}
Therefore, whether the condition is truthy or falsy, route.query.type is tracked as a dependency by watchEffect.
Also, during route navigation, even if the value of type does not change, as long as the route.query object is updated, watchEffect may run again.
How to confirm the exact reason?
You can print the number of executions and the current route value:
let count = 0
watchEffect(() => {
count++
console.log('Execution count:', count)
console.log('Current type:', route.query.type)
})
You can also use onTrack and onTrigger to inspect dependency tracking:
watchEffect(
() => {
console.log(route.query.type)
},
{
onTrack(event) {
console.log('Dependency tracked:', event)
},
onTrigger(event) {
console.log('Dependency triggered update:', event)
}
}
)
When you only care about type changes, watch is more appropriate
If the business requirement is to run the callback only when the value of route.query.type changes, you can switch to watch:
watch(
() => route.query.type,
(newType, oldType) => {
console.log('New value:', newType)
console.log('Old value:', oldType)
}
)
watch by default does not call the callback on the initial run at creation, and it only observes the value route.query.type. In contrast, watchEffect runs immediately and automatically tracks all reactive data read inside the callback.
The role of stop
The stop returned by watchEffect is used to stop the watcher. The current code extracts stop but never calls it:
const { stop } = watchEffect(() => {
// ...
})
// Call when you no longer need to watch
stop()
Watchers created in a Vue component's setup are automatically stopped when the component unmounts; if created outside a component, you need to call stop() manually depending on the situation.
Summary: This code executing twice is most commonly because watchEffect runs once immediately, then a route update triggers it again. If you only want to listen to changes in type itself, it is better to use watch(() => route.query.type, ...).
Top 1 of 2 from juejin.cn, machine-translated. The original thread is authoritative.
The fact that the check inside an if is also tracked—I hadn't noticed that before.
This is actually a bug in Vue Router, but as long as it works, it's fine. Not going to dwell on it; this is duplicate dependency collection.