fix(reactivity): differentiate shallow/deep proxies of same target when nested in reactive

fix #5271
This commit is contained in:
Evan You 2022-01-18 09:22:03 +08:00
parent 9fda9411ec
commit 9c304bfe79
2 changed files with 26 additions and 3 deletions

View File

@ -35,6 +35,26 @@ describe('shallowReactive', () => {
expect(isShallow(shallowReadonly({}))).toBe(true)
})
// #5271
test('should respect shallow reactive nested inside reactive on reset', () => {
const r = reactive({ foo: shallowReactive({ bar: {} }) })
expect(isShallow(r.foo)).toBe(true)
expect(isReactive(r.foo.bar)).toBe(false)
r.foo = shallowReactive({ bar: {} })
expect(isShallow(r.foo)).toBe(true)
expect(isReactive(r.foo.bar)).toBe(false)
})
test('should respect shallow/deep versions of same target on access', () => {
const original = {}
const shallow = shallowReactive(original)
const deep = reactive(original)
const r = reactive({ shallow, deep })
expect(r.shallow).toBe(shallow)
expect(r.deep).toBe(deep)
})
describe('collections', () => {
test('should be reactive', () => {
const shallowSet = shallowReactive(new Set())

View File

@ -8,7 +8,8 @@ import {
reactiveMap,
shallowReactiveMap,
shallowReadonlyMap,
isReadonly
isReadonly,
isShallow
} from './reactive'
import { TrackOpTypes, TriggerOpTypes } from './operations'
import {
@ -150,8 +151,10 @@ function createSetter(shallow = false) {
): boolean {
let oldValue = (target as any)[key]
if (!shallow && !isReadonly(value)) {
value = toRaw(value)
oldValue = toRaw(oldValue)
if (!isShallow(value)) {
value = toRaw(value)
oldValue = toRaw(oldValue)
}
if (!isArray(target) && isRef(oldValue) && !isRef(value)) {
oldValue.value = value
return true