What is watch?
watch is a Vue tool that lets you react to state changes:
run code when the value of a ref, reactive, computed, or even props changes.
In simple terms:
watch is a "watcher" that observes data and runs a function when it changes.
A simple example
<script setup>
import { ref, watch } from 'vue'
const count = ref(0)
watch(count, (newValue, oldValue) => {
console.log("count changed:", oldValue, "→", newValue)
})
</script>When count.value++ runs, watch fires.
What is watch for?
watch is used for side effects, for example:
- send a request when a parameter changes
- start a timer
- save data to localStorage
- track changes to props
- react to user input
- run heavy logic without blocking the UI
- debounce a search
watch is NOT for displaying data in the UI (that is what computed and template output are for).
watch syntax
1) Watch a single value
watch(source, callback)2) Watching a ref
watch(count, (newVal, oldVal) => {
console.log(newVal)
})3) Watching a reactive object
To watch the whole object, you need to use:
watch(user, (newValue, oldValue) => {
console.log("user changed", newValue)
})But there is an important nuance here, deep watching:
Vue does not track nested fields by default, you need to specify { deep: true }.
4) Watching a specific reactive property
Correct:
watch(() => user.age, (newVal) => {
console.log("Age changed:", newVal)
})5) Multiple sources
watch([firstName, lastName], ([newF, newL]) => {
console.log("First or last name changed")
})Important watch options
1) { immediate: true }
Run the watcher immediately:
watch(count, callback, { immediate: true })2) { deep: true }
For watching nested objects:
watch(user, callback, { deep: true })3) Cleaning up effects (onCleanup)
watch(id, (newId, _, onCleanup) => {
const controller = new AbortController()
fetch(`/api/${newId}`, { signal: controller.signal })
onCleanup(() => controller.abort())
})Used when canceling requests, timers, and subscriptions.
Difference between watch and computed
| Task | computed | watch |
|---|---|---|
| Get a derived value | Yes | No |
| React to a change | Not for this | Yes |
| Side effects | Not allowed | Allowed |
| Caching | Yes | No |
Example: sending a request on input
<script setup>
import { ref, watch } from 'vue'
const query = ref("")
watch(query, async (newQuery) => {
const data = await fetch(`/search?q=${newQuery}`).then(r => r.json())
console.log(data)
})
</script>Summary (in short)
watch is a reactive watcher that runs a function every time the selected data changes.
It is used for:
- side effects,
- requests,
- timers,
- synchronizing external services,
- working with
localStorage, - debouncing and complex logic.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.