11
votes

So I have been learning the Vue Composition API and was wondering what the difference between "watchEffect" and "watch" is. Watch says it's the same as the Vue 2 watch, so I'm guessing watchEffect is like the 2.0 of that? I'm wondering if there is any specific cases where one would have great advantages over the other like in the case of stopping the watchEffect and then reactivating it instead of using a boolean in a regular watch... or are they just basically different ways of writing the same thing.

Thanks!

Reference:

watcheffect: https://vue-composition-api-rfc.netlify.com/api.html#watcheffect

watch: https://vue-composition-api-rfc.netlify.com/api.html#watch

4

4 Answers

9
votes

watchEffect seems to be a simplified watch and the main differences are

  • Only accepts a function
    • watch can accept either a function or one or more reactive properties.
  • Runs immediately when defined and when reactive dependencies change
    • watch only runs when reactive dependencies change
7
votes

I would use:

  • watchEffect when I want to watch multiple reactive properties and I don't care about old values
  • watch when I want to watch one specific reactive properties and I may want old value

Note, above is what I would use them for, but may not be their only usage.

Also found in docs regarding the difference:

Compared to watchEffect, watch allows us to:

Perform the side effect lazily;
Be more specific about what state should trigger the watcher to re-run;
Access both the previous and current value of the watched state.

Source: https://composition-api.vuejs.org/api.html#watch

2
votes

watchEffect is something introduced in Vue3 with its composition api. The reason to have both watchEffect and watch, as I understand, is to keep the semantics of watch as close as possible to that of Vue2. The birth of watchEffect, if you are interested, can be traced back to here and here

As it stands today, watchEffect is an immediate/eager watch that uses a more concise/consistent syntax (consistent with computed):

  1. watchEffect does not accept explicit watch sources, but instead automatically figures out all the dependencies by executing the callback (or effect as how it is called in the source code), similar to how computed works. This is also the reason why watchEffect must run the effect immediately.
  2. watchEffect will run its effect immediately as mentioned above.
  3. watchEffect is a deep watch. This is something I am not sure whether it is intended or not. If you use a reactive object inside your effect, any change on that object will cause the effect to rerun, even if the changed property is not the one you accessed or is nested.

If Vue 3 is designed from scratch or there is no concern of maintaining backward compatibility, I would imagine there will only be watchEffect

0
votes

What helped me to understand the difference between watch and watchEffect in Vue 3 was to think about watchEffect as computed with side-effects.

The watchEffect() hook works like the computed() hook or the computed option, but instead of returning a value, you use it to trigger side-effects.

Use watch whenever you want to trigger a side-effect when a single reactive value changes.

// Triggers whenever `user` is updated.
watch(user, () => doSomething({ user: user.value, profile: profile.value }))

Use watchEffect whenever you need to watch multiple reactive values and trigger a side-effect whenever any of them is updated.

// Triggers whenever `user` *or* `profile` is updated.
watchEffect(() => doSomething({ user: user.value, profile: profile.value }))

See: watch vs. watchEffect when to use what with Vue.js