Skip to content

Commit

Permalink
fix: fix NaN comparison on state change
Browse files Browse the repository at this point in the history
fix #12595
  • Loading branch information
yyx990803 committed Jul 4, 2022
1 parent 15b9b9b commit ff5acb1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/shared/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
25 changes: 25 additions & 0 deletions test/unit/features/v3/reactivity/reactive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

0 comments on commit ff5acb1

Please sign in to comment.