question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.push when using immer as middleware does not cause a re-render

See original GitHub issue

Hello,

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:closed
  • Created 2 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
dai-shicommented, Jul 5, 2021

Good point. It somewhat accidentally works.

set((state) => {
  state.items = [...state.items, Date.now()];
  return undefined;
})

It’s returning undefined, so state = 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.

0reactions
dai-shicommented, Jul 10, 2021

Closing as resolved.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found