Created
September 15, 2016 17:12
-
-
Save jcreamer898/1928021fe8ff872e96acfd9db2de9a19 to your computer and use it in GitHub Desktop.
A way to figure out which state or props changed in any react component.
This file contains hidden or 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
const whatHasUpdated = (instance, props, state) => { | |
const changed = (obj, comparedTo) => Object.keys(obj).reduce((memo, key) => { | |
if (comparedTo[key] !== obj[key]) { | |
memo.push({ | |
key, | |
old: comparedTo[key], | |
new: obj[key], | |
}); | |
} | |
return memo; | |
}, []); | |
if (process.env.NODE_ENV === "development") { | |
const changedProps = changed(props, instance.props); | |
if (changedProps.length) console.info(...changedProps); | |
const changedState = changed(state, instance.state); | |
if (changedState.length) console.info(...changedState); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment