.push when using immer as middleware does not cause a re-render
See original GitHub issueHello,
Using immer
as a middleware for zustand works great, however when using .push
to add an item to an array a re-render does not take place.
Example:
setState(state => {
state.items.push(item);
});
The above does not work.
This does (i.e. triggers a re-render).
setState(state => {
state.items = [...state.items, item];
});
Any ideas?
Ps: .push
is immer’s the recommended way of adding to an array .
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Redux with Immer not updating component - Stack Overflow
Try adding return; at the end of your case blocks. You can read more about returning data from producers and see examples of...
Read more >Writing Reducers with Immer | Redux Toolkit - JS.ORG
It causes bugs, such as the UI not updating properly to show the latest values · It makes it harder to understand why...
Read more >Immutability in React with Immer - LogRocket Blog
You cannot simply change your state mutably, because it'll not trigger a rerender in your component. By using the setState method, you will...
Read more >Introduction to Immer - GitHub Pages
When using Immer, you don't need to learn dedicated APIs or data structures to benefit from the paradigm. With Immer you'll use plain...
Read more >Zustand - how does it work? How does it cause a re-render?
Now, what I don't understand is, how does just updating a totally separate 'state' that is not even contained in your React component...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Good point. It somewhat accidentally works.
It’s returning
undefined
, sostate = Object.assign({}, state, undefined)
will create a new state object which triggers re-render. It’s not recommended as it’s mutating old state, which might cause issues with concurrent rendering in the future.Closing as resolved.