Skip to content

Commit

Permalink
fix: should use Object.is to compare two value (#1314)
Browse files Browse the repository at this point in the history
  • Loading branch information
hchlq authored Oct 9, 2022
1 parent 43ea18e commit ae4cc7f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/vanilla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
11 changes: 11 additions & 0 deletions tests/basic.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => ({
Expand Down

0 comments on commit ae4cc7f

Please sign in to comment.