跪拜 Guibai
← All articles
Frontend · JavaScript

Why a Second Proxy Around Vue's reactive Array Silently Breaks View Updates

By 天才熊猫君 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Layering a custom Proxy over a Vue reactive object silently disables dependency tracking unless the Proxy handles `__v_raw` specially; developers who wrap reactive data for caching, logging, or decoration will hit a 'data changes but UI doesn't' bug that looks like a rendering issue, not a Proxy trap problem.

Summary

Wrapping a Vue reactive array in your own Proxy can put data into the array while the view stays frozen. In the minimal repro, `raw` holds the new item, but `len` and the `v-for` loop don't update. The write succeeds because it lands on `raw`; only the dependency notification is lost.

The loss happens in the inner `reactiveArr` set trap. It checks `target === toRaw(receiver)`, where `receiver` is the outer Proxy. `toRaw` follows `__v_raw`, but the outer Proxy's get trap treats `__v_raw` like any other object property: it gets `raw`, sees an object, and wraps it again with `wrap(raw)`. So `toRaw(proxyC)` returns `wrap(raw)` instead of `raw`, the equality check fails, and `trigger` never runs.

The fix is to special-case `__v_raw` in the outer get trap and return `t` directly. That lets `toRaw` peel through the chain back to `raw`, restoring the equality check and the trigger. Any Proxy wrapping Vue reactive objects must honor this internal contract or silently kill reactivity.

Takeaways
— The outer Proxy's get trap must pass `__v_raw` through unmodified; otherwise `toRaw` cannot unwrap to the original object.
— Vue's reactive array set trap only triggers when `target === toRaw(receiver)`, so any wrapper that changes the `receiver` identity breaks notifications.
— `proxyC.push(...)` writes through to the raw array, which makes the failure silent: the data is there, the UI just isn't told.
— Adding `if (key === '__v_raw') return t` to the outer get trap fixes the issue and restores `toRaw`.
— The `receiver` parameter of a Proxy trap propagates through nested proxies and is what Vue uses to verify the origin of a write.
— `toRaw` recursively walks `__v_raw` properties to reach the underlying raw object.
Conclusions

Vue's `__v_raw` contract is a single point of failure: any wrapper that doesn't honor it can silently break reactivity while the write still succeeds.

Because the write lands in `raw`, the symptom is a view lagging behind the data, a pattern easy to mistake for a caching or timing issue rather than a Proxy trap flaw.

Concepts & terms
__v_raw
Vue's internal property on a reactive proxy that points to the raw object being proxied. `toRaw` walks this property to unwrap nested proxies.
toRaw
A Vue utility that recursively follows `__v_raw` to return the underlying raw object from a reactive proxy.
receiver
The fourth argument passed to a Proxy trap. It identifies the object on which the operation was originally initiated, which matters when one Proxy wraps another.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗