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) 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', () => { describe('collections', () => {
test('should be reactive', () => { test('should be reactive', () => {
const shallowSet = shallowReactive(new Set()) const shallowSet = shallowReactive(new Set())

View File

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