`setState` mutates state instead of replacing it
See original GitHub issueCurrently in Preact 8.2.9
, Component.prototype.setState
mutates the previous state instead of replacing it with a patched version. This is unnecessarily inconsistent with React, can break user intuitions, can break things ported from React, and makes the second argument to shouldComponentUpdate
useless and misleading.
I discovered this the hard way when sharing an initial state constant between components, assuming it would be treated as immutable. As previously mentioned, this can break user intuitions when coming from React.
Can we fix this? I would be willing to do a PR.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Why can't I directly modify a component's state, really?
This answer is to provide enough information to not change/mutate the state directly in React. React follows Unidirectional Data Flow.
Read more >React setState() and Why State Should Not Be Mutated Directly
So, why do we use setState rather than just mutating state directly? Well, React keeps a copy of the previous state and uses...
Read more >How to accidentally mutate state. And why not to
Another way to accidentally mutate the state directly is when you use arrays. You need to be aware of which array functions are...
Read more >Common Error: Accidentally Mutating State in React
In simple terms it means that you should not modify it directly. Instead a new object should be created to set the state...
Read more >Why Not To Modify React State Directly - Dave Ceddia
So there you go: that's why you shouldn't mutate state, even if you immediately call setState. Optimized components might not re-render if you ......
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 FreeTop 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
Top GitHub Comments
Oh I guess someone beat me to it, with issue 1170.
This should be fixed in 8.3.0! Please feel free to re-open otherwise 😃