Skip to content

Commit ff5acb1

Browse files
committed
fix: fix NaN comparison on state change
fix #12595
1 parent 15b9b9b commit ff5acb1

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/shared/util.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,6 @@ export function hasChanged(x: unknown, y: unknown): boolean {
361361
if (x === y) {
362362
return x === 0 && 1 / x !== 1 / (y as number)
363363
} else {
364-
return x === x && y === y
364+
return x === x || y === y
365365
}
366366
}

test/unit/features/v3/reactivity/reactive.spec.ts

+25
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,29 @@ describe('reactivity/reactive', () => {
278278
const observed = reactive(original)
279279
expect(isReactive(observed)).toBe(false)
280280
})
281+
282+
// #12595
283+
test(`should not trigger if value didn't change`, () => {
284+
const state = reactive({
285+
foo: 1
286+
})
287+
const spy = vi.fn()
288+
effect(() => {
289+
state.foo
290+
spy()
291+
})
292+
expect(spy).toHaveBeenCalledTimes(1)
293+
294+
state.foo = 1
295+
expect(spy).toHaveBeenCalledTimes(1)
296+
297+
state.foo = NaN
298+
expect(spy).toHaveBeenCalledTimes(2)
299+
300+
state.foo = NaN
301+
expect(spy).toHaveBeenCalledTimes(2)
302+
303+
state.foo = 2
304+
expect(spy).toHaveBeenCalledTimes(3)
305+
})
281306
})

0 commit comments

Comments
 (0)