⚡ JavaScript: Fix a Proxy Trap Missing Updates From a Nested Object
A Proxy only intercepts property access on the exact object it wraps — when a property’s value is itself a nested object, reading it through the proxy returns the raw inner object, not a further proxy-wrapped version, so a mutation made directly on that nested object never triggers the outer proxy’s set trap at all, which quietly breaks any reactive system that assumes every write anywhere in the tree gets observed.
🔎 The Problem
const state = new Proxy({ user: { name: 'Ana' } }, {
set(target, key, value) {
console.log('changed:', key);
target[key] = value;
return true;
}
});
state.count = 1; // logs 'changed: count' - works
state.user.name = 'Ben'; // logs nothing at all - trap never fires,
// state.user is the raw object, not a Proxy
✅ Fix: Wrap Nested Objects in Their Own Proxy, Lazily
- Wrap nested object values in their own proxy inside the
gettrap, returning a new proxy over any object-typed property the first time it’s read, so writes anywhere in the tree route back through a trap instead of only at the top level. - Keep a
WeakMapfrom raw object to its already-created proxy so the same nested object doesn’t get wrapped twice, which would otherwise make equality checks behave inconsistently between reads. - Test specifically by mutating a deeply nested property and confirming the trap actually fires — a reactive UI that “mostly updates” is usually missing exactly this, with nested writes silently unobserved.
🚨 Why This Is Dangerous
- A state manager built on Proxy reactivity that misses nested mutations doesn’t throw an error — it just stops re-rendering for that one branch of state, which looks like a vague “sometimes stale” bug that’s hard to reproduce reliably.
A Proxy only ever watches the object you handed it — everything one property deeper is somebody else’s object until you wrap that one too.




