Skip to content

vue-deep-watch ​

A deep watcher: watch on a whole reactive() object, the props or a store (which is deep by default), or with { deep: true }.

Why: a deep watcher walks every property of the object, nested ones too, each time it runs, so it can notice a change anywhere. On a large object that is slow, and the callback runs for changes it doesn't care about: editing one field of one item runs the "reload everything" callback. Watch the fields the callback uses instead. Guide: Vue.

✕ Rejectedvue-deep-watch
ts
import { reactive, shallowRef, watch } from "vue";

export function useReport(load: (from: string, to: string) => readonly number[]) {
  const filters = reactive({ from: "", to: "", chart: { color: "blue", legend: true } });
  const rows = shallowRef<readonly number[]>([]);
  watch(filters, () => {
    rows.value = load(filters.from, filters.to);
  });
  return { filters, rows };
}

Changing the chart's colour reloads the report.

✓ Accepted0 diagnostics
ts
import { reactive, shallowRef, watch } from "vue";

export function useReport(load: (from: string, to: string) => readonly number[]) {
  const filters = reactive({ from: "", to: "", chart: { color: "blue", legend: true } });
  const rows = shallowRef<readonly number[]>([]);
  watch(() => [filters.from, filters.to] as const, ([from, to]) => {
    rows.value = load(from, to);
  });
  return { filters, rows };
}

When the callback needs the whole object, a deep watch is what you want: saving a settings record on any change, sending a form draft. Hand the callback the value, and it isn't reported:

✓ Accepted0 diagnostics
ts
import { ref, watch } from "vue";

type Settings = { theme: string; panes: { left: number; right: number } };

export function useSettings(save: (settings: Settings) => void) {
  const settings = ref<Settings>({ theme: "dark", panes: { left: 240, right: 320 } });
  watch(settings, (value) => save(value), { deep: true });
  return settings;
}

"Hands it the value" means the callback passes the watched value whole to a call: save(value), save(settings.value), JSON.stringify(value), or the callback is a function declared in the file that does (watch(settings, persist, { deep: true })). A callback that reads a few fields is reported: watch those fields.

deep: false and deep: 1 (the object's own fields, which is what a shallow copy would compare) are not reported. A larger depth is (deep: 3 walks three levels on every change), as is deep: true.

A warning in the recommended oxlint preset: on a small object, a deep watch costs little.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra