Chapter 90: Vue ‘watch’ Option

The watch option

This is the original, classic way to react to changes in reactive data (props, data, computed properties, etc.). Even in 2026 — when almost every new project uses <script setup> + watch() from Composition API — you still need to understand the old watch: { … } syntax because:

  • You will read/maintain legacy Vue 2 / early Vue 3 code
  • Many job interviews (especially in India) still ask about Options API
  • Many older tutorials, Stack Overflow answers, and plugins still show it
  • Vue Devtools still displays watchers in this style

So let’s go through it step by step — like I’m sitting next to you explaining both the old world and why we mostly moved to the new one.

1. What is the watch option? (Very simple mental model)

watch is an object where:

  • each key = what you want to observe (a property name, a dot-notation path, or a function)
  • each value = either a function (the callback) or an object with handler, deep, immediate, etc.

Vue automatically calls your callback whenever the watched value changes.

JavaScript

2. Real, Complete Example – Search + Counter + User Form

vue

4. Important Rules & Gotchas (2026 Must-Know)

Rule / Gotcha Correct Behavior / Best Practice
watch is an object watch: { count() { … } } — never an array or function
Simple property watch count(newVal, oldVal) { … } — gets new & old value
Deep watch user: { handler() { … }, deep: true } — watches nested changes
Immediate watch searchQuery: { handler() { … }, immediate: true } — runs once on create
Dot notation for nested props ‘user.age'(newAge) { … } — cleaner than deep watch for single property
Avoid arrow functions count: () => {} → this becomes undefined → bug
Still used in 2026? Yes — in legacy code, some plugins, interviews, but not in new <script setup> code

5. Modern Vue 3 – Why We Almost Never Use watch: { … } Anymore

In <script setup> (2026 standard):

vue

→ No watch object → No this → Just imported watch() function → Much more flexible (watch getters, arrays, multiple sources, onCleanup, etc.)

Quick Summary Table – watch in 2026

Question Options API (legacy) Composition API (<script setup>) What you should do in new code
How to define watcher? watch: { count() { … } } watch(count, callback) Composition API
Access in callback newVal, oldVal newVal, oldVal
Deep watch deep: true { deep: true }
Immediate watch immediate: true { immediate: true }
Still used in 2026? Yes — legacy code, plugins, interviews Almost never (except legacy) Avoid unless maintaining old code

Final 2026 Advice from Real Projects

  • In new projectsnever write watch: { … } — use watch() from Composition API
  • When you see watch as an object → it is Options API (legacy style)
  • Learn to read Options API — many jobs, open-source projects, tutorials still use it
  • Never teach beginners the watch option as primary — start with watch() in <script setup>
  • If migrating old code → gradually convert watch: { … } → watch() calls

Your mini homework:

  1. Create the search + counter component above in Options API
  2. Type in search → see debounced watcher work
  3. Change user.age → see deep watch trigger
  4. Convert it to <script setup> → compare readability & flexibility

Any part confusing? Want to see:

  • Full Options API vs Composition API watcher comparison project?
  • How watch looks in Vue Devtools?
  • Common bugs with deep watchers & performance?
  • Real component written in both styles?

Just tell me — we’ll convert and compare together step by step 🚀

You may also like...

Leave a Reply

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