DEV Community

Discussion on: Diving Into Vue 3 - Methods, Watch, and Computed

Collapse
 
selemonjr profile image
selemonjr

Is watchEffect the same as useEffect() in React ??

Collapse
 
sandramartin profile image
SandraMartin Deepgram

Both watch and watchEffect are used for running side effects when there is a change to a dependency, and it is my understanding that useEffect is also used for that purpose.

One difference is that React requires explicit tracking of dependencies, unlike Vue watchEffect, which does that for you under-the-hood.

So react useEffect is written like this:

useEffect(callback,[ dependencies]);
Enter fullscreen mode Exit fullscreen mode

But Vue watchEffect is just:

watchEffect(callback)
Enter fullscreen mode Exit fullscreen mode

I guess Vue watch is more like React useEffect since it requires you to write the dependencies first, then add a callback to run the side effect:

watch(
  [ dependencies], (callback)
  })
Enter fullscreen mode Exit fullscreen mode