Vue 3's reactivity system is one of the most elegant pieces of modern frontend engineering. At its core are two primitives:
Proxy that intercepts reads and writes.fn immediately and automatically re-runs it whenever a reactive property read inside fn is later mutated.This is the exact mechanism behind ref, computed, watch, and component re-rendering in Vue 3.
const state = reactive({ count: 0 });
effect(() => {
console.log('count is', state.count); // runs immediately → "count is 0"
});
state.count = 1; // triggers the effect → "count is 1"
state.count = 2; // triggers again → "count is 2"You need three pieces:
| Piece | Role |
|---|---|
activeEffect | A global variable pointing to the currently-running effect (or null) |
track(target, key) | Called on Proxy get — registers activeEffect as a dependency of target[key] |
trigger(target, key) | Called on Proxy set — re-runs all effects that depend on target[key] |
targetMap: WeakMap<target, Map<key, Set<effectFn>>>Implement reactive(obj), effect(fn), and _reset() (clears module-level state between tests).
The solve(op, ...args) harness is provided — do not modify it.
Sample tests