PureComponent's shallowEqual
See original GitHub issueclass App extends React.PureComponent {
state={
ele: {a:'1'}
}
trigger=()=>{
console.log('click')
this.setState({ele: {a:'1'}})
}
render() {
console.log('render')
return (
<div onClick={this.trigger}>click here to trigger setState</div>
);
}
}
A simple demo shows that click event triggers render every time. demo
However, according to source code, React.PureComponent’s shouldComponentUpdate() shallowly compares the objects like following, the click event shouldn’t trigger render.
Performs equality by iterating through keys on an object and returning false when any key has values which are not strictly equal between the arguments. Returns true when the values of all keys are strictly equal.
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Shallow Compare - React
PureComponent was introduced, shallowCompare was commonly used to achieve the same functionality as PureRenderMixin while using ES6 classes with React.
Read more >Understand PureComponent from React source code. - Derek Ji
Back to shallowEqual(), here's the source code: Firstly, it check the values with Object.is(). If they are different, there are two ...
Read more >How does shallow compare work in react - Stack Overflow
shallowCompare performs a shallow equality check on the current props and nextProps objects as well as the current state and nextState objects.
Read more >Why and How to Use PureComponent in React.js - 60devs
PureComponent, which replaces its predecessor pure-render-mixin. ... The usage of the method shallowEqual tells us that only a shallow check ...
Read more >Optimizing React.js Performance with PureComponents and ...
Before we get into the demo for this blog, let's discuss what makes a PureComponent different from a regular React Component. PureComponents and ......
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

If you mean individual properties, afaik they’re compared using
Object.isalgorithm (which is slightly different from===because it also handles NaN).Oops, yes your right 😛 not thinking clearly. The second point still holds, it’s not shallow ly equal in your example