Skip to content

Commit d4bf9bc

Browse files
authored
fix(reactivity): toRef should not wrap a ref (#2103)
1 parent aa8dc9a commit d4bf9bc

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

packages/reactivity/__tests__/ref.spec.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ describe('reactivity/ref', () => {
236236
// mutating source should trigger effect using the proxy refs
237237
a.x = 4
238238
expect(dummyX).toBe(4)
239+
240+
// should keep ref
241+
const r = { x: ref(1) }
242+
expect(toRef(r, 'x')).toBe(r.x)
239243
})
240244

241245
test('toRefs', () => {
@@ -292,12 +296,12 @@ describe('reactivity/ref', () => {
292296
test('toRefs reactive array', () => {
293297
const arr = reactive(['a', 'b', 'c'])
294298
const refs = toRefs(arr)
295-
299+
296300
expect(Array.isArray(refs)).toBe(true)
297-
301+
298302
refs[0].value = '1'
299303
expect(arr[0]).toBe('1')
300-
304+
301305
arr[1] = '2'
302306
expect(refs[1].value).toBe('2')
303307
})

packages/reactivity/src/ref.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ export function toRef<T extends object, K extends keyof T>(
168168
object: T,
169169
key: K
170170
): Ref<T[K]> {
171-
return new ObjectRefImpl(object, key) as any
171+
return isRef(object[key])
172+
? object[key]
173+
: (new ObjectRefImpl(object, key) as any)
172174
}
173175

174176
// corner case when use narrows type

0 commit comments

Comments
 (0)