watch() is Vue's API for reacting to reactive state changes with access to both the new and old values.
const state = reactive({ count: 0 });
watch(
() => state.count, // source — a getter function
(newVal, oldVal) => { // callback — called on each change
console.log(`${oldVal} → ${newVal}`);
}
);
state.count = 5; // logs "0 → 5"
state.count = 10; // logs "5 → 10"Key behaviour:
Implement watch(source, callback), reactive(obj), and _reset().
The solve(op, ...args) harness is provided — do not modify it.
Sample tests