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.