Skip to content

Watchers

watch runs side effects when sources change — useful for async syncing. This lesson covers practical patterns, syntax, and mistakes to avoid.

Responding to Changes with watch

watch runs side effects when sources change — useful for async syncing.

Prefer computed for pure derived data; watch for effects.

watch(userId, async (id) => {
  user.value = await fetchUser(id)
})

Fetch when an id changes.

Options

immediate, deep, flush
  • Prefer Composition API + script setup.
  • Use computed for derived UI.
  • Keep side effects intentional.
  • Practice in a small Vite app.

Watchers Cheatsheet

Quick reference for patterns covered in this lesson.

Item Example Purpose
watch effect API
watchEffect auto deps API
immediate run now Option
deep nested Option
stop handle Cleanup
flush timing Advanced

Cleanup

Stop watchers when appropriate; handle races in async callbacks.

Common Mistakes

  • Watching everything deeply by default.
  • Duplicating computed logic in watchers.

Key Takeaways

  • Watchers is important in Vue apps.
  • Practice in SFCs.
  • Prefer clear naming.
  • Check Vue docs for details.

Pro Tip

When fetching, watch an explicit id source for clarity.