consider using Immer
See original GitHub issuePeople get immutability wrong all the time (self included). Even the ones that know what they’re doing often have to write some really messy code when deep state updates are involved.
Immer seems to achieve the best of both worlds. We should consider bundling it in (and possible have a flag that allows turning it off for people that prefer not to use it).
move: (G, ctx) {
G.hand++;
G.deck--;
}
vs.
move: (G, ctx) {
return {
...G,
hand: G.hand + 1,
deck: G.deck - 1,
};
}
Open questions:
- How do we indicate invalid moves ( #292 ) in this approach?
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Using Immer with React: a Simple Solutions for Immutable ...
Based on my experience with Immer, I believe it is a great option to use with React. It will simplify your code and...
Read more >Using TypeScript or Flow | Immer - GitHub Pages
The Immer package ships with type definitions inside the package, which should be picked up by TypeScript and Flow out of the box...
Read more >Better Reducers With Immer - Smashing Magazine
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 >Immutability in React with Immer - LogRocket Blog
To better deal with state data, a library was created to help us, called Immer. Immer was created to help us to have...
Read more >Writing Reducers with Immer | Redux Toolkit - JS.ORG
Our answer is always the same: Immer is required in RTK, and that is not going to change. It's worth going over the...
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
Supported in
0.28.0
.I can see the advantage of having current code errantly mutating state being safe once this is implemented. Immer automatically binds
this
to the draft ofG
inside of produce, so I would expect both should work.