Watchers let you react to changes in data and run code when something changes. They help you do extra work outside the main display, like fetching data or logging.
You want to fetch new data when a user changes a search term.
You need to save form data automatically when a user edits it.
You want to run a function when a specific value changes, like toggling a setting.
You want to log or track changes in a variable for debugging.
You want to trigger animations or side effects when data updates.
Syntax
Vue
watch(source, callback, options?)
// source: a reactive reference or getter function// callback: function(newValue, oldValue) to run on change// options: optional object like { immediate: true }
The source can be a reactive variable or a function returning a value.
The callback runs every time the source changes, receiving new and old values.
Watchers are good for side effects but avoid heavy logic inside them.
Use immediate: true if you want the watcher to run once right away.
Remember to clean up watchers if you create them manually to avoid memory leaks.
Summary
Watchers run code when reactive data changes.
They are useful for side effects like fetching or logging.
Use watch with a source and a callback function.
Practice
(1/5)
1. What is the main purpose of a watch in Vue?
easy
A. To run code when reactive data changes
B. To define a new component
C. To style elements dynamically
D. To create a template for HTML
Solution
Step 1: Understand what watch does
A watcher observes reactive data and triggers code when that data changes.
Step 2: Compare options to watcher purpose
Only To run code when reactive data changes describes running code on reactive data change, which matches watcher behavior.
Final Answer:
To run code when reactive data changes -> Option A
Quick Check:
Watcher = run code on data change [OK]
Hint: Watchers react to data changes by running code [OK]
Common Mistakes:
Confusing watchers with components
Thinking watchers style elements
Assuming watchers create templates
2. Which of the following is the correct syntax to watch a reactive variable count in Vue 3 Composition API?
easy
A. watch(count, (newVal, oldVal) => { console.log(newVal) })
B. watch({ count }, (newVal, oldVal) => { console.log(newVal) })
C. watch('count', (newVal, oldVal) => { console.log(newVal) })
D. watch(() => count, (newVal, oldVal) => { console.log(newVal) })
Solution
Step 1: Recall Vue 3 watch syntax for reactive refs
In Vue 3 Composition API, to watch a ref's value, you typically pass a getter function like watch(() => count, callback) or watch(() => count.value, callback). Passing the ref directly watches the ref object, not its value.
Step 2: Check each option
watch(count, ...) watches the ref object, not its value. watch({ count }, ...) passes an object, invalid. watch('count', ...) uses invalid string. watch(() => count, ...) watches the value returned by the function, which is the ref's value.
B. Missing function to return count in watch source
C. Ref must be declared with reactive() instead of ref()
D. watch must be imported from 'vue-router'
Solution
Step 1: Check watch source argument
In Vue 3 Composition API, to watch a ref, you must pass a function returning the ref's value, like watch(() => count.value, callback).
Step 2: Identify the error
The code passes count.value directly, which is incorrect. It should be watch(() => count.value, ...).
Final Answer:
Missing function to return count in watch source -> Option B
Quick Check:
Watch source must be a function returning value [OK]
Hint: Watch source must be a function returning reactive value [OK]
Common Mistakes:
Passing count.value (primitive) directly instead of ref or getter
Thinking callback needs two parameters always
Importing watch from wrong package
5. You want to watch a reactive object user and run a side effect only when the user.name changes, ignoring other properties. Which watcher setup is correct?