What is the difference Between watch and watchEffect in Vue.js 3?

Harish Kumar · · 2759 Views

There are a few cases where we may need to track a reactive property, and we can do this by utilizing a Vue watcher.

In Vue.js 3, in addition to the watch method, there’s a new watchEffect method.

How Does Vue watchEffect Work?

watchEffect is one of the approaches to track reactive dependencies in Vue3. We can simply write a method utilizing reactive properties, and at whatever point one of their values updates, our method will run.

One thing to note is that watchEffect will likewise run immediately at whatever point the component is initialized in addition to running when the dependency changes, so careful when attempting to access the DOM before it's mounted.

watchEffect Example:

let’s assume that we have a type of reactive userID property and simply need to log whenever it changes.

The code would resemble this:

setup () {
    const userID = ref(0)

    watchEffect(() => console.log(userID))
    setTimeout(() => {
      userID.value = 1
    }, 1000)

      * LOG
      * 0 
      * 1

    return {

So right when our component is instated, our watchEffect will run and log the initial value. Then, at whatever point the value of userID changes, our watchEffect will be triggered.

 Difference Between watch vs watchEffect 

So for what reason does this new watchEffect technique even exist when there's a watch method? 

There are some key differences. 

watchEffect will run a method at whatever point any of its dependencies are changed, while watch tracks a particular reactive property and will possibly run when that property changes. 

On the other hand, the watch tracks a particular property or properties and will possibly run when it changes. Likewise, with the way that a watch method is set up, it contains the old value of the property just as its updated value.

Additionally, the watch is lazy, so it will possibly trigger when the dependency changes. watchEffect runs immediately when the component is created and afterward tracks dependency.

So watch is useful for when: 

  1. You need to control which dependencies trigger the method. 

  2. You need access to the previous value.

  3. You need to lazily perform the method.

Obviously, I can't tell you which is best for your task. However, ensure you know the reasons why you're making a coding design choice.


Please login or create new account to add your comment.

You may also like:

The Ultimate Guide to Mobile Game Development in 2023

Did you know that a game is a composition of all the modern aspects that are important for humans in today's world. It includes programming, art design, music, writing, and much (...)
Kenny William

How 2D Game Development Company is Helpful To Develop Excellent Mobile Games in 2023?

A reliable 2D game development company can be very helpful in developing outstanding mobile games because of their specialized expertise and experience in the domain. Here are (...)
Kenny William

Learn PHP Development from beginning.

PHP stance Hypertext Preprocessor, it's server-side scripting laungage and easy to use. PHP  also well  known for its speed, simplicity, flexibility features that have made it (...)
Programmer Desk

How to Create a Mobile App Using JavaScript: Step by Step Guide for 2023

How to Create a Mobile App Using JavaScript: Step by Step Guide for 2023
James Smith

Types of Instacart Clone Apps To Power Your Next Grocery Business

The Instacart app has been widely successful in delivering products on time. Not only are the items delivered safely, but they are also picked up by a specially assigned shopper. (...)
Narola Infotech

Vuex Tutorial: Learn State management in Vue.js using Vuex

The objective of this Vuex tutorial is to give you an essential understanding of state management in Vue.js using Vuex by creating a relatable example. By the end of this tutorial, (...)
Harish Kumar