diff --git a/src/vanilla.ts b/src/vanilla.ts index 34d6facb45..65cc64537d 100644 --- a/src/vanilla.ts +++ b/src/vanilla.ts @@ -72,7 +72,7 @@ const createStoreImpl: CreateStoreImpl = (createState) => { typeof partial === 'function' ? (partial as (state: TState) => TState)(state) : partial - if (nextState !== state) { + if (!Object.is(nextState, state)) { const previousState = state state = replace ?? typeof nextState !== 'object' diff --git a/tests/basic.test.tsx b/tests/basic.test.tsx index 43729f4356..24ecab7bc6 100644 --- a/tests/basic.test.tsx +++ b/tests/basic.test.tsx @@ -437,6 +437,17 @@ it('can set the store', () => { expect(getState().value).toBe(5) }) +it('both NaN should not update', () => { + const { setState, subscribe } = create(() => NaN) + + const fn = jest.fn() + subscribe(fn) + + setState(NaN) + + expect(fn).not.toBeCalled() +}) + it('can set the store without merging', () => { const { setState, getState } = create<{ a: number } | { b: number }>( (_set) => ({