Skip to content

Commit

Permalink
fix(reactivity): pass oldValue to computed getter (#11813)
Browse files Browse the repository at this point in the history
close #11812
  • Loading branch information
yangxiuxiu1115 authored Sep 5, 2024
1 parent c518517 commit 98864a7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions packages/reactivity/__tests__/computed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down

0 comments on commit 98864a7

Please sign in to comment.