Vue3 Fundamentals ยท Reactivity

How Vue tracks dependencies

the reactivity system

How Vue tracks dependencies

Every API in this Module rests on one mechanism. Understanding it explains why a plain variable never updates and why a ref does.

When a component renders, Vue runs its render function inside an effect. As the effect reads reactive values, Vue records each one โ€” this is tracking. A ref or reactive object is a proxy that intercepts those reads and adds the current effect to a set of subscribers.

When a value changes, the proxy triggers that set: every effect that read the value is scheduled to run again. The component re-renders, reads the new value, and the cycle repeats. A plain variable is not a proxy, so nothing is tracked and nothing is triggered.

Two details matter in practice. Tracking is per property: an effect that reads state.count re-runs when count changes, not when a sibling property does. And dependencies are collected on each run: an effect only depends on what it actually read last time, which is why a conditional branch can change what a watchEffect watches.

computed is the same mechanism with caching: it tracks its dependencies, and recomputes only when one of them has changed and the value is read again.

You are about to answer a short quiz on how Vue tracks dependencies.

  1. Vue records a dependency when an effect reads a reactive value.
  2. When a reactive value changes, the proxy ___ the effects that read it.
  3. A plain variable is tracked just like a `ref`.
  4. Reactivity tracks dependencies per ___ , so reading `state.count` does not subscribe to a sibling property.
  5. An effect's dependencies are collected again on each run.
  6. `computed` caches its result and recomputes only when a dependency changes.