Skip to content

Commit 92b7eb1

Browse files
edwardnycneiyichao03
andauthored
fix(proxyRefs): When using proxyRefs, the internal variable composition-api.refKey is exposed on the object itself #817 (#818)
Co-authored-by: neiyichao03 <[email protected]>
1 parent 68b5d97 commit 92b7eb1

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/reactivity/ref.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { RefKey } from '../utils/symbols'
2-
import { proxy, isPlainObject, warn } from '../utils'
2+
import { proxy, isPlainObject, warn, def } from '../utils'
33
import { reactive, isReactive, shallowReactive } from './reactive'
44
import { readonlySet } from '../utils/sets'
55

@@ -193,6 +193,8 @@ export function proxyRefs<T extends object>(
193193
}
194194
const value: Record<string, any> = reactive({ [RefKey]: objectWithRefs })
195195

196+
def(value, RefKey, value[RefKey], false)
197+
196198
for (const key of Object.keys(objectWithRefs)) {
197199
proxy(value, key, {
198200
get() {

test/v3/reactivity/ref.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import {
1313
isReactive,
1414
shallowRef,
1515
proxyRefs,
16+
ShallowUnwrapRef,
1617
} from '../../../src'
18+
import { RefKey } from '../../../src/utils/symbols'
1719

1820
describe('reactivity/ref', () => {
1921
it('should hold a value', () => {
@@ -379,15 +381,23 @@ describe('reactivity/ref', () => {
379381
y: ref('foo'),
380382
},
381383
}
382-
const p = proxyRefs(a)
384+
const reactiveA = {
385+
[RefKey]: a,
386+
...a,
387+
}
388+
const p = proxyRefs(a) as ShallowUnwrapRef<typeof reactiveA>
383389
expect(p.x).toBe(1)
384390
expect(p.obj.y).toBe('foo')
391+
expect(p[RefKey]).toBe(a)
392+
expect(Object.keys(p)).toStrictEqual(['x', 'obj'])
385393

386394
// @ts-expect-error
387395
p.obj.y = 'bar'
388396
p.x = 2
389397
expect(a.x).toBe(2)
390398
expect(a.obj.y).toBe('bar')
399+
expect(p[RefKey]).toBe(a)
400+
expect(Object.keys(p)).toStrictEqual(['x', 'obj'])
391401

392402
const r = reactive({ k: 'v' })
393403
const s = proxyRefs(r)

0 commit comments

Comments
 (0)