From d4bf9bcbb430fa0168ca48039579d59e6789c6f5 Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Tue, 15 Sep 2020 15:29:38 +0100 Subject: [PATCH] fix(reactivity): `toRef` should not wrap a `ref` (#2103) --- packages/reactivity/__tests__/ref.spec.ts | 10 +++++++--- packages/reactivity/src/ref.ts | 4 +++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/reactivity/__tests__/ref.spec.ts b/packages/reactivity/__tests__/ref.spec.ts index 9ef6c4e4..883af0f8 100644 --- a/packages/reactivity/__tests__/ref.spec.ts +++ b/packages/reactivity/__tests__/ref.spec.ts @@ -236,6 +236,10 @@ describe('reactivity/ref', () => { // mutating source should trigger effect using the proxy refs a.x = 4 expect(dummyX).toBe(4) + + // should keep ref + const r = { x: ref(1) } + expect(toRef(r, 'x')).toBe(r.x) }) test('toRefs', () => { @@ -292,12 +296,12 @@ describe('reactivity/ref', () => { test('toRefs reactive array', () => { const arr = reactive(['a', 'b', 'c']) const refs = toRefs(arr) - + expect(Array.isArray(refs)).toBe(true) - + refs[0].value = '1' expect(arr[0]).toBe('1') - + arr[1] = '2' expect(refs[1].value).toBe('2') }) diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 64d9f107..6596ecdd 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -168,7 +168,9 @@ export function toRef( object: T, key: K ): Ref { - return new ObjectRefImpl(object, key) as any + return isRef(object[key]) + ? object[key] + : (new ObjectRefImpl(object, key) as any) } // corner case when use narrows type