Skip to content

Commit

Permalink
fix(reactivity): retain readonly proxies when setting as reactive pro…
Browse files Browse the repository at this point in the history
…perty

fix #4986
  • Loading branch information
yyx990803 committed Nov 25, 2021
1 parent 820a143 commit d145128
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
9 changes: 9 additions & 0 deletions packages/reactivity/__tests__/readonly.spec.ts
Expand Up @@ -465,4 +465,13 @@ describe('reactivity/readonly', () => {
'Set operation on key "randomProperty" failed: target is readonly.'
).toHaveBeenWarned()
})

// #4986
test('setting a readonly object as a property of a reactive object should retain readonly proxy', () => {
const r = readonly({})
const rr = reactive({}) as any
rr.foo = r
expect(rr.foo).toBe(r)
expect(isReadonly(rr.foo)).toBe(true)
})
})
5 changes: 3 additions & 2 deletions packages/reactivity/src/baseHandlers.ts
Expand Up @@ -7,7 +7,8 @@ import {
readonlyMap,
reactiveMap,
shallowReactiveMap,
shallowReadonlyMap
shallowReadonlyMap,
isReadonly
} from './reactive'
import { TrackOpTypes, TriggerOpTypes } from './operations'
import {
Expand Down Expand Up @@ -146,7 +147,7 @@ function createSetter(shallow = false) {
receiver: object
): boolean {
let oldValue = (target as any)[key]
if (!shallow) {
if (!shallow && !isReadonly(value)) {
value = toRaw(value)
oldValue = toRaw(oldValue)
if (!isArray(target) && isRef(oldValue) && !isRef(value)) {
Expand Down

1 comment on commit d145128

@Magua-Q
Copy link

@Magua-Q Magua-Q commented on d145128 Dec 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.