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
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 Pattern4---5# Vue Watchers Pattern67> React to data changes with watch and watchEffect for side effects and async operations89## When to Use1011- You need to perform side effects (API calls, localStorage writes, analytics) when reactive data changes12- Implementing debounced search, auto-save, or data synchronization13- Watching route params or store state for navigation-driven updates1415## Instructions16171. Use `watch(source, callback)` when you need the old and new values.182. Use `watchEffect(callback)` when you want automatic dependency tracking.193. Pass `{ deep: true }` to watch nested object mutations.204. Always clean up side effects in the `onCleanup` callback to prevent leaks.2122```typescript23import { ref, watch, watchEffect } from 'vue';2425const query = ref('');2627// Explicit source — gives old and new28watch(query, (newVal, oldVal) => {29 console.log(`Query changed: ${oldVal} → ${newVal}`);30});3132// Auto-tracked dependencies33watchEffect((onCleanup) => {34 const controller = new AbortController();35 fetch(`/api/search?q=${query.value}`, { signal: controller.signal });36 onCleanup(() => controller.abort());37});38```39405. Use `{ immediate: true }` with `watch` to run the callback immediately on setup.416. Watch multiple sources: `watch([ref1, ref2], ([new1, new2], [old1, old2]) => { ... })`.4243## Details4445Vue 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.4647**Trade-offs:**4849- `watchEffect` can trigger unexpectedly if it accesses reactive data you did not intend to track50- Deep watching large objects is expensive — Vue must traverse the entire object tree51- Watchers run asynchronously by default (after DOM updates) — use `{ flush: 'sync' }` if you need synchronous execution (rare)5253**When NOT to use:**5455- For derived/computed values — use `computed()` instead, which caches the result56- For template-reactive data — just use `ref`/`reactive` directly; Vue re-renders automatically57- When a simple event handler would suffice — do not use a watcher to react to user clicks5859## Source6061https://patterns.dev/vue/watchers-pattern6263## Process64651. Read the instructions and examples in this document.662. Apply the patterns to your implementation, adapting to your specific context.673. Verify your implementation against the details and edge cases listed above.6869## Harness Integration7071- **Type:** knowledge — this skill is a reference document, not a procedural workflow.72- **No tools or state** — consumed as context by other skills and agents.7374## Success Criteria7576- The patterns described in this document are applied correctly in the implementation.77- Edge cases and anti-patterns listed in this document are avoided.
Run npx skillmds@latest add intense-visions/vue-watchers-pattern in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
Vue Watchers Pattern It is listed under Web & Frontend on SkillMD.
This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Intense-Visions (@intense-visions) published this skill. Their other Agent Skills are listed on their SkillMD profile.