diff --git a/packages/reactivity/__tests__/reactive.spec.ts b/packages/reactivity/__tests__/reactive.spec.ts index cbc8c871..8020bbf8 100644 --- a/packages/reactivity/__tests__/reactive.spec.ts +++ b/packages/reactivity/__tests__/reactive.spec.ts @@ -131,6 +131,21 @@ describe('reactivity/reactive', () => { expect(typeof obj.b).toBe(`number`) }) + test('should allow setting property from a ref to another ref', () => { + const foo = ref(0) + const bar = ref(1) + const observed = reactive({ a: foo }) + const dummy = computed(() => observed.a) + expect(dummy.value).toBe(0) + + // @ts-ignore + observed.a = bar + expect(dummy.value).toBe(1) + + bar.value++ + expect(dummy.value).toBe(2) + }) + test('non-observable values', () => { const assertValue = (value: any) => { reactive(value) diff --git a/packages/reactivity/src/baseHandlers.ts b/packages/reactivity/src/baseHandlers.ts index f8452a35..65838c13 100644 --- a/packages/reactivity/src/baseHandlers.ts +++ b/packages/reactivity/src/baseHandlers.ts @@ -66,14 +66,14 @@ function createGetter(isReadonly = false, shallow = false) { return res } + !isReadonly && track(target, TrackOpTypes.GET, key) + if (shallow) { - !isReadonly && track(target, TrackOpTypes.GET, key) return res } if (isRef(res)) { if (targetIsArray) { - !isReadonly && track(target, TrackOpTypes.GET, key) return res } else { // ref unwrapping, only for Objects, not for Arrays. @@ -81,7 +81,6 @@ function createGetter(isReadonly = false, shallow = false) { } } - !isReadonly && track(target, TrackOpTypes.GET, key) return isObject(res) ? isReadonly ? // need to lazy access readonly and reactive here to avoid