Chapter 69: Vue $watch() Method

Watch()).

This is the tool you reach for when you want to run some code automatically every time a specific piece of reactive data changes.

It’s like saying:

“Hey Vue, please keep an eye on this value/ref/computed/getter… and the moment it changes (even deeply nested changes), run my callback function.”

In Vue 2 / early Vue 3 (Options API), it was written as $watch inside the component object. In modern Vue 3 (2026 standard), especially with <script setup>, we use the imported watch() function — which is much cleaner, more flexible, and type-safe.

1. The Two Worlds: Options API vs Composition API

Old style (Options API – still in many legacy projects)

vue

Modern style (<script setup> – what you should use in 2026)

vue

2. The Anatomy of watch() (Modern Composition API)

watch() has three main forms:

TypeScript

Callback arguments

TypeScript

Most useful options (third argument)

Option Type Default What it does / When to use
immediate boolean false Run callback immediately on setup (great for initial fetch)
deep boolean false Watch nested changes in objects/arrays (expensive – use wisely)
flush ‘pre’ / ‘post’ / ‘sync’ ‘pre’ When callback runs: before DOM (‘pre’), after DOM (‘post’), synchronously (‘sync’)
once boolean false Run callback only once
onCleanup function Register cleanup function (cancel timers, subscriptions…)

3. Real, Practical Examples (copy-paste ready)

Example 1 – Search input with debounced API call (very common pattern)

vue

Example 2 – Watch deep object + cleanup

vue

Quick Reference Table – watch() in 2026

What you want to watch Syntax (Composition API) Options you usually need
Single ref watch(count, callback)
Reactive object (deep) watch(user, callback, { deep: true }) deep: true
Computed property watch(fullName, callback)
Getter expression watch(() => user.age * 2, callback)
Multiple sources watch([count, user.age], callback)
Run immediately + deep watch(source, callback, { immediate: true, deep: true }) immediate, deep
Cleanup (cancel fetch, timer…) watch(source, (newV, oldV, onCleanup) => { onCleanup(() => {…}) }) onCleanup

Pro Tips from Real Projects (Hyderabad 2026)

  • Prefer getter function (watch(() => user.age, …)) over deep watching whole object — much more performant
  • Always clean up in onCleanup or onBeforeUnmount — avoid memory leaks (timers, event listeners, AbortController…)
  • Use flush: ‘post’ when you need DOM to be updated before your callback
  • For debouncing/throttling → wrap in setTimeout or use lodash.debounce inside watch
  • For search / filter inputs → watch + debounce is the classic pattern
  • Don’t overuse deep watch — it can be expensive on large objects

Your mini practice task:

  1. Create search input → watch it with 400ms debounce → fake API call → show results
  2. Add a deep watch on a user object → log changes
  3. Add cleanup for a timer that starts when user changes
  4. Try immediate: true → see callback runs on mount

Any part confusing? Want full examples for:

  • Watch + AbortController for cancelling fetch?
  • Watch multiple sources with different flush modes?
  • Watch vs watchEffect comparison?
  • Real search + pagination with watch?

Just tell me — we’ll build the next reactive watcher together 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *