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

Harish Kumar · · 3733 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 {
      date
    }
  }

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.

0

Please login or create new account to add your comment.

0 comments
You may also like:

How to Use useEffect in React: Tips, Examples, and Pitfalls to Avoid

useEffect is one of the most commonly used hooks in React, enabling you to manage side effects like fetching data, subscribing to events, or manipulating the DOM. However, improper (...)
Harish Kumar

15 Must-Know TypeScript Features to Level Up Your Development Skills

TypeScript has become the go-to tool for developers building scalable, maintainable JavaScript applications. Its advanced features go far beyond basic typing, giving developers (...)
Harish Kumar

JavaScript Best Practices: Tips for Writing Clean and Maintainable Code

JavaScript is one of the most versatile and widely used programming languages today, powering everything from simple scripts to complex web applications. As the language continues (...)
Harish Kumar

Ditch jQuery: Vanilla JS Alternatives You Need to Know

jQuery revolutionized web development by simplifying DOM manipulation, event handling, and animations. However, modern JavaScript (ES6 and beyond) now provides many built-in methods (...)
Harish Kumar

Shallow Copy vs Deep Copy in JavaScript: Key Differences Explained

When working with objects and arrays in JavaScript, it's crucial to understand the difference between shallow copy and deep copy. These concepts dictate how data is duplicated (...)
Harish Kumar

A Beginner’s Guide to Efficient Memory Use in JavaScript

Managing memory efficiently in JavaScript applications is essential for smooth performance, especially for large-scale or complex applications. Poor memory handling can lead to (...)
Harish Kumar