Skip to content

Commit d145128

Browse files
committed
fix(reactivity): retain readonly proxies when setting as reactive property
fix vuejs#4986
1 parent 820a143 commit d145128

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

packages/reactivity/__tests__/readonly.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,4 +465,13 @@ describe('reactivity/readonly', () => {
465465
'Set operation on key "randomProperty" failed: target is readonly.'
466466
).toHaveBeenWarned()
467467
})
468+
469+
// #4986
470+
test('setting a readonly object as a property of a reactive object should retain readonly proxy', () => {
471+
const r = readonly({})
472+
const rr = reactive({}) as any
473+
rr.foo = r
474+
expect(rr.foo).toBe(r)
475+
expect(isReadonly(rr.foo)).toBe(true)
476+
})
468477
})

packages/reactivity/src/baseHandlers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
readonlyMap,
88
reactiveMap,
99
shallowReactiveMap,
10-
shallowReadonlyMap
10+
shallowReadonlyMap,
11+
isReadonly
1112
} from './reactive'
1213
import { TrackOpTypes, TriggerOpTypes } from './operations'
1314
import {
@@ -146,7 +147,7 @@ function createSetter(shallow = false) {
146147
receiver: object
147148
): boolean {
148149
let oldValue = (target as any)[key]
149-
if (!shallow) {
150+
if (!shallow && !isReadonly(value)) {
150151
value = toRaw(value)
151152
oldValue = toRaw(oldValue)
152153
if (!isArray(target) && isRef(oldValue) && !isRef(value)) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy