fix(reactivity): pass oldValue to computed getter (#11813)

close #11812
This commit is contained in:
yangxiuxiu 2024-09-05 16:10:37 +08:00 committed by GitHub
parent c518517cb3
commit 98864a7ef5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 1 deletions

View File

@ -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)

View File

@ -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++