Skip to content

Instantly share code, notes, and snippets.

@fatso83
Created January 26, 2018 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fatso83/37e8af63fff96bd966001b56032a1f1c to your computer and use it in GitHub Desktop.
Save fatso83/37e8af63fff96bd966001b56032a1f1c to your computer and use it in GitHub Desktop.
/**
* Add these to a class you want to debug
*
* It should show you when and why a render() is called for
* Use that for a custom shouldComponentUpdate() implementation
*
* Of course don't use this implementation, as deep compares are
* expensive, compared to the often cheap renders.
*/
compare(nextProps){
console.warn('doing compare!')
Object.keys(nextProps).forEach(prop => {
if(nextProps[prop] !== this.props[prop]) console.log('new prop: ', prop);
});
}
shouldComponentUpdate(nextProps, nextState){
this.compare(nextProps)
const propDiff = diff(this.props, nextProps)
const stateDiff = diff(this.state, nextState)
const propLen = Object.keys(propDiff).length;
const stateLen = Object.keys(stateDiff).length;
return !!(propLen || stateLen);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment