mirror of
https://github.com/pmndrs/zustand.git
synced 2025-12-08 19:45:52 +00:00
fix(vanilla): unexpected null state update behavior (#2213)
* fix: unexpected null state update behavior * chore: lint code * test: add test for setting state to null --------- Co-authored-by: wulimao <tao@trlab.com>
This commit is contained in:
parent
f5561dfb05
commit
949b505a4b
@ -73,7 +73,7 @@ const createStoreImpl: CreateStoreImpl = (createState) => {
|
||||
if (!Object.is(nextState, state)) {
|
||||
const previousState = state
|
||||
state =
|
||||
replace ?? typeof nextState !== 'object'
|
||||
replace ?? (typeof nextState !== 'object' || nextState === null)
|
||||
? (nextState as TState)
|
||||
: Object.assign({}, state, nextState)
|
||||
listeners.forEach((listener) => listener(state, previousState))
|
||||
|
||||
@ -112,6 +112,24 @@ it('can set the store without merging', () => {
|
||||
expect(getState()).toEqual({ b: 2 })
|
||||
})
|
||||
|
||||
it('can set the object store to null', () => {
|
||||
const { setState, getState } = createStore<{ a: number } | null>(() => ({
|
||||
a: 1,
|
||||
}))
|
||||
|
||||
setState(null)
|
||||
|
||||
expect(getState()).toEqual(null)
|
||||
})
|
||||
|
||||
it('can set the non-object store to null', () => {
|
||||
const { setState, getState } = createStore<string | null>(() => 'value')
|
||||
|
||||
setState(null)
|
||||
|
||||
expect(getState()).toEqual(null)
|
||||
})
|
||||
|
||||
it('works with non-object state', () => {
|
||||
const store = createStore<number>(() => 1)
|
||||
const inc = () => store.setState((c) => c + 1)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user