From ff5acb12cfa64f975f216a55aafa976b07052665 Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 4 Jul 2022 10:37:05 +0800 Subject: [PATCH] fix: fix NaN comparison on state change fix #12595 --- src/shared/util.ts | 2 +- .../features/v3/reactivity/reactive.spec.ts | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/shared/util.ts b/src/shared/util.ts index a171a743c14..0b9cf7f06ec 100644 --- a/src/shared/util.ts +++ b/src/shared/util.ts @@ -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 } } diff --git a/test/unit/features/v3/reactivity/reactive.spec.ts b/test/unit/features/v3/reactivity/reactive.spec.ts index e5e381d71df..f636d44d4a1 100644 --- a/test/unit/features/v3/reactivity/reactive.spec.ts +++ b/test/unit/features/v3/reactivity/reactive.spec.ts @@ -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) + }) })