zustand
zustand copied to clipboard
fix: delete root keys with immer middleware
Related Issues or Discussions
Fixes #2480
Summary
Currently when calling setState
with the immer
middleware, replace=false
is assumed by default. This means that the old state is merged with immer's produce
output. I don't think there is any reason to merge those states: immer's produce
already returns a smart version of the whole state where copies are used whenever possible. The merging is mostly equivalent to replace=True
, except when keys are deleted: the old deleted keys get merged with the new state.
I'm also assuming that when using the immer
middleware, we always want to do something like
store.setState((state) => { state.x = 3})
and never
store.setState({x: 3})
so I simplified the middleware consequently.
I added a test to reproduce the bug and some other simple tests.
Someone with more experience with the immer
middleware should double-check all this! There could very well be usage patterns that I'm not aware of.
Check List
- [x]
yarn run prettier
for formatting code and docs
@dai-shi @dbritto-dev Let me know if I can improve anything in the PR.