fix: should use Object.is to compare two value (#1314)

This commit is contained in:
huangcheng 2022-10-09 09:10:37 +08:00 committed by GitHub
parent 43ea18e768
commit ae4cc7f1b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -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'

View File

@ -437,6 +437,17 @@ it('can set the store', () => {
expect(getState().value).toBe(5)
})
it('both NaN should not update', () => {
const { setState, subscribe } = create<number>(() => 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) => ({