Created
January 26, 2018 12:49
-
-
Save fatso83/37e8af63fff96bd966001b56032a1f1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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