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.

`setState` mutates state instead of replacing it

See original GitHub issue

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

github_iconTop GitHub Comments

1reaction
KevinDoughtycommented, Jul 26, 2018

Oh I guess someone beat me to it, with issue 1170.

0reactions
developitcommented, Aug 5, 2018

This should be fixed in 8.3.0! Please feel free to re-open otherwise 😃

Read more comments on GitHub >

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

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