Created
March 16, 2023 14:29
-
-
Save CezaryDanielNowak/a943d7a6b32402e3edc06a7dd652185c 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
/** | |
* How to use: | |
* In the render function, include | |
* | |
* window.whyRerender && window.whyRerender(this.props, this.state); | |
*/ | |
window.whyRerender = (props, state) => { | |
if (window.whyRerender.prevProps) { | |
const propsDiff = Object.keys(props).map((propName) => { | |
if (props[propName] !== window.whyRerender.prevProps[propName]) { | |
return propName; | |
} | |
}).filter(Boolean); | |
const stateDiff = Object.keys(state).map((stateName) => { | |
if (state[stateName] !== window.whyRerender.prevState[stateName]) { | |
return stateName; | |
} | |
}).filter(Boolean); | |
console.log('[whyRerender]', propsDiff, stateDiff) | |
} | |
window.whyRerender.prevProps = { ...props }; | |
window.whyRerender.prevState = { ...state }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
...
whyRerender(this.props, this.state, this.className);