diff --git a/packages/reactivity/__tests__/computed.spec.ts b/packages/reactivity/__tests__/computed.spec.ts index 08b034f79f1..c3409eee77e 100644 --- a/packages/reactivity/__tests__/computed.spec.ts +++ b/packages/reactivity/__tests__/computed.spec.ts @@ -33,6 +33,20 @@ describe('reactivity/computed', () => { expect(cValue.value).toBe(1) }) + it('pass oldValue to computed getter', () => { + const count = ref(0) + const oldValue = ref() + const curValue = computed(pre => { + oldValue.value = pre + return count.value + }) + expect(curValue.value).toBe(0) + expect(oldValue.value).toBe(undefined) + count.value++ + expect(curValue.value).toBe(1) + expect(oldValue.value).toBe(0) + }) + it('should compute lazily', () => { const value = reactive<{ foo?: number }>({}) const getter = vi.fn(() => value.foo) diff --git a/packages/reactivity/src/effect.ts b/packages/reactivity/src/effect.ts index a22990f6729..88493e4e9a9 100644 --- a/packages/reactivity/src/effect.ts +++ b/packages/reactivity/src/effect.ts @@ -381,7 +381,7 @@ export function refreshComputed(computed: ComputedRefImpl): false | undefined { try { prepareDeps(computed) - const value = computed.fn() + const value = computed.fn(computed._value) if (dep.version === 0 || hasChanged(value, computed._value)) { computed._value = value dep.version++