Skip to content

Lesson 1 of 9 · 24m

Vue Reactivity: refs, reactive, and What Actually Tracks

How Vue's dependency tracking really works, where refs unwrap and where they don't, and the reactivity-loss bugs that cost teams days.

Vue's reactivity system is excellent when you understand its contract and maddening when you treat it as magic. The contract is this: Vue tracks property access on reactive objects and .value access on refs. Anything outside those tracked reads — destructuring a reactive object, passing a primitive around, mutating an object Vue never proxied — silently falls out of the system.

ref boxes a value; reactive proxies an object

import { ref, reactive } from "vue";

const count = ref(0);
count.value++;

const state = reactive({ count: 0 });
state.count++;

Use ref for primitives and single values. Use reactive when the object itself is the unit of state. The important distinction is not taste — it is identity. A ref keeps reactivity when you pass it somewhere because the box itself is stable. A reactive object's individual properties lose reactivity when copied into plain variables.

Destructuring is where reactivity dies

const state = reactive({ visible: false, count: 0 });

// broken: plain copies
const { visible, count } = state;

// fixed: reactive references
const { visible, count } = toRefs(state);

This is the classic production bug: the template updates once during setup, then never again because setup destructured reactive state into local constants. If you need named values, use toRefs. If you need the whole object, keep it whole.

Computed values are derived state, not methods

const items = ref<Item[]>([]);
const total = computed(() =>
  items.value.reduce((sum, item) => sum + item.price, 0),
);

A computed is cached and lazily re-evaluated when its dependencies change. A method runs every time the render runs. If the value is derived from reactive state, do not store a copy in another ref — compute it. Duplicating derived state is how Vue apps end up with "sometimes the UI is one update behind" bugs.

Watchers are for effects, not derivations

watch(
  () => route.params.id,
  async (id, previous) => {
    if (!id || id === previous) return;
    await loadUser(String(id));
  },
  { immediate: true },
);

Reach for watch when you need to react to a change: reset a form, load data, write analytics, synchronize with something outside Vue. Do not use it to calculate state that could be computed, and do not use it as a substitute for events. "When field A changes, update field B" often means the inputs share a model badly designed upstream.

Field notes

  • Template refs auto-unwrap; script code still needs .value. New hires trip over this daily.
  • shallowRef is right for large objects replaced wholesale, especially third-party instances or immutable snapshots.
  • markRaw non-Vue objects — maps, editor instances, chart objects — before putting them in reactive state. Proxying a complex object can break it and is wasted work.
  • If reactivity "randomly stops," look first for destructuring, then for values copied into plain module-level variables.

Was this lesson helpful?

Sign in to rate