diff --git a/packages/reactivity/__tests__/reactive.spec.ts b/packages/reactivity/__tests__/reactive.spec.ts index aabd95456..a23f2066f 100644 --- a/packages/reactivity/__tests__/reactive.spec.ts +++ b/packages/reactivity/__tests__/reactive.spec.ts @@ -301,6 +301,13 @@ describe('reactivity/reactive', () => { expect(() => markRaw(obj)).not.toThrowError() }) + test('should not markRaw object as reactive', () => { + const a = reactive({ a: 1 }) + const b = reactive({ b: 2 }) as any + b.a = markRaw(toRaw(a)) + expect(b.a === a).toBe(false) + }) + test('should not observe non-extensible objects', () => { const obj = reactive({ foo: Object.preventExtensions({ a: 1 }), diff --git a/packages/reactivity/src/reactive.ts b/packages/reactivity/src/reactive.ts index 729c85496..c549d7291 100644 --- a/packages/reactivity/src/reactive.ts +++ b/packages/reactivity/src/reactive.ts @@ -279,16 +279,16 @@ function createReactiveObject( ) { return target } - // target already has corresponding Proxy - const existingProxy = proxyMap.get(target) - if (existingProxy) { - return existingProxy - } // only specific value types can be observed. const targetType = getTargetType(target) if (targetType === TargetType.INVALID) { return target } + // target already has corresponding Proxy + const existingProxy = proxyMap.get(target) + if (existingProxy) { + return existingProxy + } const proxy = new Proxy( target, targetType === TargetType.COLLECTION ? collectionHandlers : baseHandlers,