Add Usage Guide page on writing reducers with Immer
See original GitHub issueSeems like a lot of the questions we get are around how to use Immer-powered reducers effectively. Common issues include:
- How to reset state
- Trying to do
state = someValue
and thinking that will replace the state - Debug logging proxies and needing to use
current(state)
Definitely worth a docs page that covers these things and more.
Also see #875 for some related thoughts.
Additional notes:
- Can do
Object.assign(state, data)
- General examples of mutation, and mixing and matching mutations and immutable logic (
state.items = state.items.filter()
) - Mutate or return new state, but not both
Issue Analytics
- State:
- Created 3 years ago
- Comments:8
Top Results From Across the Web
Writing Reducers with Immer | Redux Toolkit - JS.ORG
Redux Toolkit's createReducer and createSlice automatically use Immer internally to let you write simpler immutable update logic using "mutating ...
Read more >Simplify Redux reducers with immer - Arnaud Dostes - Medium
A simple React/Redux application using Immer. Please clap if you like the story, and leave suggestions and questions in the comments! References:
Read more >Better Reducers With Immer
In this article, we're going to learn how to use Immer to write reducers. When working with React, we maintain a lot of...
Read more >Add Usage Guide page on writing reducers with Immer
Add Usage Guide page on writing reducers with Immer · How to reset state · Trying to do state = someValue and thinking...
Read more >Modern Redux with Redux Toolkit
However, for real applications, you should use Redux Toolkit to write your ... If you write mutating logic in reducers without Immer, it...
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
Makes sense. I’d like to make one last push for a discussion of these within a “immer and redux” document.
Immer does two great things, it reduces boilerplate related to maintaining immutability AND it allows working with mutable things in a way that is effectively immutable, at least with respect to reference equality.
I think RTK’s approach to immer is “when you use this you’ll have nicer shorter code and less bugs “.
But immer itself says “you can use me to work with maps and sets”
So my first point in favor of a discussion of it in these specific docs is that a reader needs a reconciliation between what redux says and what immer says. Why do the immer folks say “yeah, use Maps, it just works” but the redux folks say “use immer, but don’t use Maps”.
The second reason to include a discussion is that these patterns are immensely useful, and a more nuanced discussion will likely give users “permission “ to experiment and see if they fit in their own workflow. For us, giving up on time travel debugging and persistence was an easy choice because we could express things we really needed very easily with Maps. But we always loved in fear because the redux docs said “just don’t” even though immer says “you can use this stuff, it just works” and it really does.
In other words, probably tens of thousands of people make decisions based on these docs and it’s worth giving them the nuance to make a fully informed choice .
To summarize, I’m not suggesting that redux condone the use of map and friends. But, immer had changed the technical details behind the blanket no. In a discussion of immer and redux, it’s helpful to acknowledge what immer states and map those statements to redux recommendations with enough detail for the end user to make an informed decision
On Sat 20. Feb 2021 at 21:33, Mark Erikson notifications@github.com wrote:
I know. I always thought that’s forwned upon because those lead to state mutation which is a no-no. But immer solves that. So, when discussing immer, I’d like to know if the official position has changed and why or why not. I think it would be useful to more people than just myself. The official docs say
To which I say 1) Immer makes the UI update as expected 2) The developer convenience I get from sets is greater than what I get from Redux DevTools.
The second point is a tradeoff, which developers can make. The first point is pretty important, I think Immer removed a barrier in state management. So either I’m totally wrong (and probably not the only one) or it’s a fair point and it would be helpful if a discussion of immer touched on way not to use Maps/Sets anyway .