A doubt behaviour using the PureComponent
See original GitHub issueWhat is the current behavior?
I’ve met a strange behaviour using PureComponent:
It’s normal to unincrease the number when click the add button as follow(shallowEqual return true):
class B extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
count: 0
};
this.click = this.click.bind(this);
}
click() {
this.setState({
count: ++this.state.count
});
}
render() {
return (
<div>
<button onClick={this.click}>add</button>
<div>{this.state.count}</div>
</div>
);
}
}
but I’m doubt it’s normal to increase the number when using like follow: link
click() {
const state = Object.assign({}, this.state);
this.setState({
count: ++state.count
});
}
In my option, the effect of { count: ++this.state.count } is same as { count: ++state.count }. I don’t know why the second way can do it ? Thanks for replying.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Why are all the PureComponent children of a container ...
I modified the Todo from a functional component to PureComponent class so that shouldComponentUpdate() returns false if all the prop references ...
Read more >React PureComponents and Hooks function components | by July ...
Migrating from PureComponent to functional component, wrapping the component with React.memo provides the same re-rendering behavior as PureComponent. The props ...
Read more >State Management in Pure React with Hooks: useEffect
The useEffect() Hook takes two arguments. The first argument is a function in which you can perform your side effects, and the second...
Read more >Nadia Makarevich on LinkedIn: PureComponents vs Functional ...
Probably something on data fetching in React, but I'm open to other ideas as well: if you or your team is struggling with...
Read more >Optimizing React component event handlers - Nicholas Tsim
The React team are aware that this can be a performance trap, so they encourage us to use memo or PureComponent on 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
@MuYunyun, your
++this.state.count
have already change the state to new version@AliasT It’s surprised. 👍 I understand, Thanks.
I’ve writen a post about this issue in Chinese.