A watcher in Vue is a special function that 'watches' a reactive value or expression and runs code when that value changes. It's used to perform side effects like fetching data or updating something outside the component.
Click to reveal answer
beginner
How do you define a watcher using the Composition API in Vue 3?
You use the watch function by passing the reactive source and a callback function that runs when the source changes. For example:
Why use watchers for side effects instead of computed properties?
Computed properties are for deriving values based on reactive data and are cached. Watchers are for running code that causes side effects, like API calls or manual DOM updates, which should not be done inside computed properties.
Click to reveal answer
intermediate
What does the immediate option do in a Vue watcher?
The immediate option makes the watcher callback run right away when the watcher is created, not just when the watched value changes later. This is useful to run side effects on initial setup.
Click to reveal answer
intermediate
How can you stop a watcher in Vue?
When you call watch, it returns a stop function. Calling this function stops the watcher from running in the future, which helps avoid memory leaks or unwanted side effects.
Click to reveal answer
What is the main purpose of a watcher in Vue?
ATo run code when reactive data changes
BTo create new components
CTo style elements
DTo define routes
✗ Incorrect
Watchers run code when reactive data changes, often for side effects.
Which Vue API function is used to create a watcher in the Composition API?
AwatchEffect
Bwatch
Ccomputed
Dref
✗ Incorrect
watch is used to watch reactive sources and run side effect code.
What does setting immediate: true do in a watcher?
APrevents the watcher from running
BRuns the watcher only once
CRuns the watcher callback immediately on setup
DDelays the watcher callback
✗ Incorrect
The immediate option runs the watcher callback right away when created.
Which of these is NOT a good use case for watchers?
ACalculating a value based on other data
BUpdating the page title
CFetching data when a value changes
DLogging changes to the console
✗ Incorrect
Calculations should be done with computed properties, not watchers.
How do you stop a watcher from running in Vue?
ARemove the component
BSet the watcher to null
CUse <code>unwatch()</code> function
DCall the stop function returned by watch
✗ Incorrect
The watch function returns a stop function to stop the watcher.
Explain how watchers help manage side effects in Vue applications.
Think about when and why you want to run extra code after data changes.
You got /5 concepts.
Describe how to create a watcher using Vue 3 Composition API and how to stop it.
Focus on the syntax and lifecycle of a watcher.
You got /5 concepts.
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?