BeginnerCoder

Lesson 15 — Watchers

Run code when a value changes

watch(source, callback) calls the callback every time the source ref changes, giving you the new value (and the old one). Use it for side effects — saving, logging, fetching — rather than for values you display, which belong in computed.

watch(query, (now, before) => {
  console.log("search changed from", before, "to", now)
})

Why it matters: "save the draft whenever it changes" and "reload when the filter changes" are watchers.

Want it another way?
Task

Watch count and add "count is now X" to the log on every change. After two clicks the list shows two lines, ending with "count is now 2".

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.