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.

PureComponent's shallowEqual

See original GitHub issue
class 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:closed
  • Created 5 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
gaearoncommented, Aug 27, 2018

If you mean individual properties, afaik they’re compared using Object.is algorithm (which is slightly different from === because it also handles NaN).

1reaction
jquensecommented, Aug 24, 2018

Oops, yes your right 😛 not thinking clearly. The second point still holds, it’s not shallow ly equal in your example

Read more comments on GitHub >

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

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