Skip to content

Commit

Permalink
fix(reactivity): toRef should not wrap a ref (#2103)
Browse files Browse the repository at this point in the history
  • Loading branch information
pikax authored Sep 15, 2020
1 parent aa8dc9a commit d4bf9bc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 7 additions & 3 deletions packages/reactivity/__tests__/ref.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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')
})
Expand Down
4 changes: 3 additions & 1 deletion packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ export function toRef<T extends object, K extends keyof T>(
object: T,
key: K
): Ref<T[K]> {
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
Expand Down

0 comments on commit d4bf9bc

Please sign in to comment.