Categories
Vue Answers

How to watch for prop changes with the Vue.js Composition API?

Spread the love

Sometimes, we want to watch for prop changes with the Vue.js Composition API.

In this article, we’ll look at how to watch for prop changes with the Vue.js Composition API.

How to watch for prop changes with the Vue.js Composition API?

To watch for prop changes with the Vue.js Composition API, we can call the watch function.

For instance, we write

watch(
  () => props.selected,
  (selection, prevSelection) => {
    /* ... */
  }
);

to call watch to watch the value of the selected prop.

And then we can get the current value of selected from selection and the previous value of it from prevSelection.

Conclusion

To watch for prop changes with the Vue.js Composition API, we can call the watch function.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.