Vue Watchers Pattern
React to data changes with watch and watchEffect for side effects and async operations
When to Use
- You need to perform side effects (API calls, localStorage writes, analytics) when reactive data changes
- Implementing debounced search, auto-save, or data synchronization
- Watching route params or store state for navigation-driven updates
Instructions
- Use
watch(source, callback) when you need the old and new values.
- Use
watchEffect(callback) when you want automatic dependency tracking.
- Pass
{ deep: true } to watch nested object mutations.
- Always clean up side effects in the
onCleanup callback to prevent leaks.
import { ref, watch, watchEffect } from 'vue';
const query = ref('');
// Explicit source — gives old and new
watch(query, (newVal, oldVal) => {
console.log(`Query changed: ${oldVal} → ${newVal}`);
});
// Auto-tracked dependencies
watchEffect((onCleanup) => {
const controller = new AbortController();
fetch(`/api/search?q=${query.value}`, { signal: controller.signal });
onCleanup(() => controller.abort());
});
- Use
{ immediate: true } with watch to run the callback immediately on setup.
- Watch multiple sources:
watch([ref1, ref2], ([new1, new2], [old1, old2]) => { ... }).
Details
Vue provides two watcher APIs. watch() is explicit — you declare what to watch and get old/new values. watchEffect() is implicit — it automatically tracks any reactive dependency accessed during execution. Both return a stop function to cancel the watcher.
Trade-offs:
watchEffect can trigger unexpectedly if it accesses reactive data you did not intend to track
- Deep watching large objects is expensive — Vue must traverse the entire object tree
- Watchers run asynchronously by default (after DOM updates) — use
{ flush: 'sync' } if you need synchronous execution (rare)
When NOT to use:
- For derived/computed values — use
computed() instead, which caches the result
- For template-reactive data — just use
ref/reactive directly; Vue re-renders automatically
- When a simple event handler would suffice — do not use a watcher to react to user clicks
Source
https://patterns.dev/vue/watchers-pattern
Process
- Read the instructions and examples in this document.
- Apply the patterns to your implementation, adapting to your specific context.
- Verify your implementation against the details and edge cases listed above.
Harness Integration
- Type: knowledge — this skill is a reference document, not a procedural workflow.
- No tools or state — consumed as context by other skills and agents.
Success Criteria
- The patterns described in this document are applied correctly in the implementation.
- Edge cases and anti-patterns listed in this document are avoided.
1---2name: vue-watchers-pattern3description: Vue Watchers Pattern4metadata:5 source: https://skillmd.com/skills/intense-visions/vue-watchers-pattern6---7# Vue Watchers Pattern89> React to data changes with watch and watchEffect for side effects and async operations1011## When to Use1213- You need to perform side effects (API calls, localStorage writes, analytics) when reactive data changes14- Implementing debounced search, auto-save, or data synchronization15- Watching route params or store state for navigation-driven updates1617## Instructions18191. Use `watch(source, callback)` when you need the old and new values.202. Use `watchEffect(callback)` when you want automatic dependency tracking.213. Pass `{ deep: true }` to watch nested object mutations.224. Always clean up side effects in the `onCleanup` callback to prevent leaks.2324```typescript25import { ref, watch, watchEffect } from 'vue';2627const query = ref('');2829// Explicit source — gives old and new30watch(query, (newVal, oldVal) => {31 console.log(`Query changed: ${oldVal} → ${newVal}`);32});3334// Auto-tracked dependencies35watchEffect((onCleanup) => {36 const controller = new AbortController();37 fetch(`/api/search?q=${query.value}`, { signal: controller.signal });38 onCleanup(() => controller.abort());39});40```41425. Use `{ immediate: true }` with `watch` to run the callback immediately on setup.436. Watch multiple sources: `watch([ref1, ref2], ([new1, new2], [old1, old2]) => { ... })`.4445## Details4647Vue provides two watcher APIs. `watch()` is explicit — you declare what to watch and get old/new values. `watchEffect()` is implicit — it automatically tracks any reactive dependency accessed during execution. Both return a stop function to cancel the watcher.4849**Trade-offs:**5051- `watchEffect` can trigger unexpectedly if it accesses reactive data you did not intend to track52- Deep watching large objects is expensive — Vue must traverse the entire object tree53- Watchers run asynchronously by default (after DOM updates) — use `{ flush: 'sync' }` if you need synchronous execution (rare)5455**When NOT to use:**5657- For derived/computed values — use `computed()` instead, which caches the result58- For template-reactive data — just use `ref`/`reactive` directly; Vue re-renders automatically59- When a simple event handler would suffice — do not use a watcher to react to user clicks6061## Source6263https://patterns.dev/vue/watchers-pattern6465## Process66671. Read the instructions and examples in this document.682. Apply the patterns to your implementation, adapting to your specific context.693. Verify your implementation against the details and edge cases listed above.7071## Harness Integration7273- **Type:** knowledge — this skill is a reference document, not a procedural workflow.74- **No tools or state** — consumed as context by other skills and agents.7576## Success Criteria7778- The patterns described in this document are applied correctly in the implementation.79- Edge cases and anti-patterns listed in this document are avoided.