fix: fix NaN comparison on state change

fix #12595
This commit is contained in:
Evan You 2022-07-04 10:37:05 +08:00
parent 15b9b9b19d
commit ff5acb12cf
2 changed files with 26 additions and 1 deletions

View File

@ -361,6 +361,6 @@ export function hasChanged(x: unknown, y: unknown): boolean {
if (x === y) {
return x === 0 && 1 / x !== 1 / (y as number)
} else {
return x === x && y === y
return x === x || y === y
}
}

View File

@ -278,4 +278,29 @@ describe('reactivity/reactive', () => {
const observed = reactive(original)
expect(isReactive(observed)).toBe(false)
})
// #12595
test(`should not trigger if value didn't change`, () => {
const state = reactive({
foo: 1
})
const spy = vi.fn()
effect(() => {
state.foo
spy()
})
expect(spy).toHaveBeenCalledTimes(1)
state.foo = 1
expect(spy).toHaveBeenCalledTimes(1)
state.foo = NaN
expect(spy).toHaveBeenCalledTimes(2)
state.foo = NaN
expect(spy).toHaveBeenCalledTimes(2)
state.foo = 2
expect(spy).toHaveBeenCalledTimes(3)
})
})