How Vue reactivity works

Intermediate · 35 min

How Vue tracks dependencies with Proxies, getters, and effects, why updates are batched, and how the engine changed in Vue 3.5 and 3.6.

Before thisComputed propertiesWatchers with watch and watchEffect

Vue Germany · 35 min

How Vue reactivity works

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Vue GermanyReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Vue's reactivity is built from three moves: when an effect reads reactive state, Vue tracks that dependency; when the state is written, Vue triggers every effect that depended on it; effects re-run and track afresh.
  • `reactive()` intercepts reads and writes with a JavaScript Proxy; `ref()` does the same with a getter and setter on `.value`. Both call into the same track-and-trigger machinery.
  • A component's render function, every `computed`, and every watcher are all effects. That is why the template, derived values, and side effects all stay in sync with no explicit subscriptions.
  • Triggered component updates are queued and flushed together in a microtask, so ten writes in one handler cause one re-render. `await nextTick()` waits for that flush when you need the updated DOM.
  • Unlike React, which re-runs a component function and diffs the output on every state change, Vue knows which components read the changed state and re-renders only those.
  • The engine has been rewritten for speed without changing the API: Vue 3.5 cut its memory use with version counting and linked lists of dependencies, and Vue 3.6 (in release candidate as of November 2026) rebuilds it on the alien-signals design.

The question underneath everything so far

The previous lessons asked you to trust a few behaviours. Changing a ref updates the page. A computed knows when it is stale. watchEffect somehow knows what it read. None of that is magic. It all comes from one small mechanism, and once you can picture it, the rules (why destructuring breaks reactivity, why watchEffect misses reads after an await) stop being rules to memorise and become consequences you can predict.

The problem the mechanism solves is plain. In ordinary JavaScript, const total = price * quantity is computed once. Change price later and total does not care. A UI framework needs the opposite: change price, and everything that used it updates. To do that, the framework has to know who used what.

Three moves: track, trigger, re-run

Vue’s answer has three parts.

  1. Effects. An effect is a function Vue runs while watching what it touches. A component’s render function is an effect. So is every computed getter and every watcher.
  2. Track. While an effect runs, every time it reads a piece of reactive state, Vue records “this effect depends on this property.”
  3. Trigger. When reactive state is written, Vue looks up every effect that depended on that property and schedules each one to run again. Re-running it tracks its dependencies afresh, so a v-if branch that stops reading something also stops depending on it.

Here is a stripped-down version of the idea, close to what the video live-codes:

let activeEffect = null
const targetMap = new WeakMap() // object -> Map(key -> Set(effects))

function track(target, key) {
  if (!activeEffect) return
  let depsMap = targetMap.get(target)
  if (!depsMap) targetMap.set(target, (depsMap = new Map()))
  let dep = depsMap.get(key)
  if (!dep) depsMap.set(key, (dep = new Set()))
  dep.add(activeEffect)
}

function trigger(target, key) {
  targetMap.get(target)?.get(key)?.forEach(effect => effect())
}

function effect(fn) {
  const run = () => {
    activeEffect = run
    fn()
    activeEffect = null
  }
  run()
}

The WeakMap keyed by object means a reactive object that is no longer used can still be garbage collected. The Map, Set, and weak collections lesson covers why.

Where track and trigger get called

Vue has to call track on every read and trigger on every write without you doing anything. That is what reactive() and ref() are for.

reactive() wraps an object in a Proxy:

function reactive(target) {
  return new Proxy(target, {
    get(obj, key, receiver) {
      track(obj, key)
      const value = Reflect.get(obj, key, receiver)
      return typeof value === 'object' && value !== null ? reactive(value) : value
    },
    set(obj, key, value, receiver) {
      const result = Reflect.set(obj, key, value, receiver)
      trigger(obj, key)
      return result
    },
  })
}

Notice that nested objects are wrapped lazily, at the moment they are read. That is how deep reactivity works without walking the whole structure upfront. The real implementation also caches proxies, so the same object always gets the same proxy, and has traps for has, deleteProperty, and iteration.

ref() cannot use a Proxy on a primitive, so it is an object with an accessor:

class RefImpl {
  constructor(value) { this._value = value }
  get value() { track(this, 'value'); return this._value }
  set value(next) { this._value = next; trigger(this, 'value') }
}

Now the earlier gotchas explain themselves. const { count } = state performs one read through the proxy and copies out a number. Later writes to that number never pass through a trap. watchEffect loses track of reads after an await because activeEffect has already been reset by the time the async part resumes. And Vue 2’s famous “add a new property and nothing happens” problem existed because it used Object.defineProperty on each existing key. A Proxy intercepts any key, including new ones.

A computed is an effect that is also a dependency

A computed sits in the middle of the chain. Its getter is an effect that tracks the state it reads. Its own .value is something other effects track. When a dependency changes, the computed does not recompute immediately. It marks itself dirty and notifies its own dependents. The next read recomputes. That is where both laziness and caching come from.

Batching and nextTick

If every trigger re-rendered immediately, five writes in one click handler would mean five renders. Instead, when a component’s render effect is triggered, Vue puts an update job in a queue (deduplicated, so a component is queued once) and flushes the queue in a microtask after your synchronous code finishes. That is the event loop doing the batching.

The visible consequence: right after a state change, the DOM has not been patched yet.

import { nextTick } from 'vue'

async function addItem(item) {
  items.value.push(item)
  await nextTick() // wait for Vue to flush the queued render
  listEl.value.lastElementChild.scrollIntoView()
}

Why this differs from React

React re-runs a component function when its state changes, then diffs the new output against the old. By default every child re-renders too, unless you memoise. That model is explained in why React re-renders.

Vue’s <script setup> runs once. Afterwards, only render effects re-run, and only for components whose render actually read the changed state. A parent changing a value its child never sees does not re-render the child. This is why Vue apps rarely need the manual memoisation React apps lean on. The fine-grained tracking does that bookkeeping for you.

The engine keeps getting faster

The API above has stayed the same since Vue 3.0, but the engine under it has been rewritten twice.

  • Vue 3.5 (2024) restructured dependency tracking around version counters and doubly linked lists in place of the Sets in the sketch above, cutting the reactivity system’s memory use by roughly half.
  • Vue 3.6 rebuilds the core on the design of alien-signals, a signals library by Vue team member Johnson Chu. It uses a push-pull model: writes push “you may be stale” notifications through the graph, and values are pulled and recomputed only when read. As of November 2026, 3.6 is in release candidate; 3.5 is the current stable line.

None of this changes how you write code. Alexander Lichter’s video in the “go deeper” links explains the push-pull idea and the benchmarks behind it.

What the video covers

Marc Backes’ vuejs.de Conf talk explains the theory and then live-codes a simplified Vue 3 reactivity engine on stage, building up effects, track, trigger, and a Proxy-based reactive() step by step. Watching the pieces get typed out is the fastest way to make the model stick. His engine is the classic, simplified shape shown above. It is pre-3.5, so it does not show version counting or the alien-signals core; the prose covers those changes. For the source of truth, Evan You’s two-hour RenderATL workshop builds a reactivity system from scratch and maps it onto Vue’s real implementation.

Where this fits

Knowing that deep reactivity means a proxy at every level makes it obvious when you would want less of it. That is shallow reactivity and escape hatches.

Check yourself

  1. 01Inside an effect, Vue sees `state.items.length` being read. What does it record, and what later write will cause the effect to re-run?

    During the read, the Proxy's get trap calls track, which records that the currently running effect depends on the `items` property of `state`, and then on the `length` of that array. Any later write that triggers either dependency re-runs the effect: assigning a new array to `state.items`, or pushing, splicing, or otherwise changing the array's length.

  2. 02Why couldn't Vue 2 detect `this.user.nickname = 'Ada'` when `nickname` was a new property, while Vue 3 can?

    Vue 2 made objects reactive by converting each existing property into a getter/setter with `Object.defineProperty` at creation time. A property added later never got that treatment, so writes to it went unnoticed, hence `Vue.set`. Vue 3 wraps the whole object in a Proxy, whose traps intercept access to any key, including ones that did not exist when the proxy was created.

  3. 03A click handler sets `count.value++` five times. How many times does the component re-render, and why?

    Once. Each write triggers the component's render effect, but Vue does not re-render immediately. It puts the component's update job in a queue, deduplicated so the same job is only queued once, and flushes the queue in a microtask after the handler finishes. By then `count` holds its final value, and a single render picks it up.

  4. 04Why is reading the DOM right after `items.value.push(newItem)` likely to show the old list, and what fixes it?

    The push triggers an update that is queued rather than applied immediately, so the DOM has not been patched yet when the next line runs. `await nextTick()` returns a promise that resolves after Vue flushes its update queue, at which point the DOM reflects the new item.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark