Why a Second Proxy Around Vue's reactive Array Silently Breaks View Updates
Why Does Wrapping a Proxy Around Vue's reactive Break Reactivity?
First look at a minimal demo.
<template>
<button @click="add">添加</button>
<div>len: {{ proxyC.length }}</div>
<div v-for="item in proxyC" :key="item">{{ item }}</div>
</template>
<script setup>
import { reactive } from 'vue'
const raw = []
const reactiveArr = reactive(raw)
const cache = new WeakMap()
function wrap(value) {
if (value === null || typeof value !== 'object') return value
if (cache.has(value)) return cache.get(value)
const p = new Proxy(value, {
get(t, key) {
const val = t[key]
if (val !== null && typeof val === 'object') return wrap(val)
return val
},
})
cache.set(value, p)
return p
}
const proxyC = wrap(reactiveArr)
function add() {
proxyC.push('item-' + (proxyC.length + 1))
}
</script>
Name them consistently:
raw: the lowest-level raw array.reactiveArr: the reactive array returned byreactive(raw). It is also a Proxy wrappingraw.proxyC: the outer Proxy returned bywrap(reactiveArr), wrappingreactiveArr.
Observation: clicking Add makes raw.length become 1, the data is there; but the page's len doesn't change, and v-for doesn't add an item.
The data is written, but the view doesn't update.
Below we only trace proxyC.push('item-1').
1. proxyC.push reads the push on reactiveArr
The first step of proxyC.push('item-1') is reading proxyC.push.
proxyC's target is reactiveArr, so reading push falls through to reactiveArr. Since reactiveArr is a Vue reactive array, reading an array method like push returns Vue's rewritten push. It roughly does two things:
toRaw(this) // first unwrap back to the raw object
this.push(...) // the write still happens on `this`
This step is read-only, no write.
2. The set trap of reactiveArr — just look at the signature
When this push is called, this is proxyC. It eventually writes the index and length on proxyC:
proxyC[0] = 'item-1'
proxyC.length = 1
proxyC is only a layer around reactiveArr; writing properties eventually lands on the inner reactiveArr. Since reactiveArr itself is a Proxy, writing to it enters the set trap of reactiveArr. Its signature is:
set(target, key, value, receiver) {
...
}
target, key, value are straightforward. The fourth parameter receiver is the initiator of this write operation.
With a single-layer proxy:
const p = new Proxy(rawObj, {
set(target, key, value, receiver) {
console.log(receiver === p) // true
return Reflect.set(target, key, value, receiver)
},
})
p.a = 2
With multiple layers, the initiator is passed along as receiver:
const inner = new Proxy(rawObj, {
set(target, key, value, receiver) {
console.log(receiver === outer) // true
return Reflect.set(target, key, value, receiver)
},
})
const outer = new Proxy(inner, {})
outer.a = 2
If an intermediate layer writes Reflect.set(target, key, value) without passing receiver, the receiver breaks at that layer and becomes target.
3. Simplified set trap of reactiveArr
set(target, key, value, receiver) {
// target = raw, receiver = proxyC
target[key] = value
if (target === toRaw(receiver)) {
trigger(target, 'set', key, value, oldValue)
}
}
The first line writes the value to target; in this demo target is raw.
Then it decides whether to notify the view. First look at toRaw:
function toRaw(observed) {
const next = observed && observed['__v_raw']
return next ? toRaw(next) : observed
}
It peels off layers using __v_raw. On a Vue reactive proxy, reading __v_raw returns the raw object it wraps:
const rawObj = { a: 1 }
const reactiveObj = reactive(rawObj)
reactiveObj.__v_raw === rawObj // true
trigger(target) notifies the subscribers of target. It only notifies if this write originated from target itself:
// Direct write to reactiveArr: initiator is reactiveArr itself
reactiveArr[0] = 'item-1'
// target = raw, receiver = reactiveArr
// toRaw(receiver) = raw
// raw === raw, true, trigger(raw)
// In the demo, the write is to proxyC: initiator is the outer proxy
proxyC[0] = 'item-1'
// target = raw, receiver = proxyC
// Need to check whether toRaw(proxyC) is raw
Now we need to know what toRaw(proxyC) actually is. Let's analyze that next.
4. Why toRaw(proxyC) can't peel down to raw
toRaw(proxyC) reads proxyC.__v_raw, hitting the get trap of proxyC:
get(t, key) {
// t = reactiveArr, key = '__v_raw'
const val = t[key]
if (val !== null && typeof val === 'object') return wrap(val)
return val
}
The get trap of proxyC reads __v_raw from reactiveArr, which per the contract above returns raw. But once it gets raw, it sees it's an object and wraps it again: wrap(raw). So:
toRaw(proxyC) === wrap(raw) // not raw
Back to proxyC[0] = 'item-1':
proxyC[0] = 'item-1'
// target = raw, receiver = proxyC
// Need to check toRaw(proxyC) == raw
target === toRaw(receiver)
// raw === wrap(raw)
// false
The check fails, so trigger is skipped. The data is written into raw, but nobody notifies the view, so the page doesn't move.
5. Fix: let the get trap of proxyC pass __v_raw straight through
The problem is the get trap of proxyC: it treats __v_raw like an ordinary object property. reactiveArr returns raw, and then it wraps raw again.
Vue's contract for __v_raw is clear: accessing this special property returns the target object of the current layer of the proxy. Follow that semantics:
get(t, key) {
if (key === '__v_raw') return t
const val = t[key]
if (val !== null && typeof val === 'object') return wrap(val)
return val
}
Here t is reactiveArr. So proxyC.__v_raw returns reactiveArr, and toRaw continues down: reactiveArr.__v_raw returns raw. Thus:
toRaw(proxyC) === raw
Back to proxyC[0] = 'item-1', the check is restored:
target === toRaw(receiver)
// raw === raw, true
trigger(raw, ...) runs normally, and the view updates.
Top 1 of 2 from juejin.cn, machine-translated. The original thread is authoritative.
Panda is still hanging in there, and I'm crying to death.
Persisting at what? You mean posting articles?