Skip to content

Instantly share code, notes, and snippets.

@poislagarde
Last active February 1, 2017 16:39
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 poislagarde/551870e36fd549d07d620348b23a6824 to your computer and use it in GitHub Desktop.
Save poislagarde/551870e36fd549d07d620348b23a6824 to your computer and use it in GitHub Desktop.
React debug component update
componentWillUpdate (nextProps, nextState) {
const componentName = this.constructor.name
const { props, state } = this
const logUpdate = (location, key, value, nextValue) => {
console.groupCollapsed(`${componentName} - ${location}.${key} changed!`)
console.log('before: ', value[key])
console.log('after: ', nextValue[key])
console.groupEnd()
}
// shallow compare props
for (let key in props) {
if (!(key in nextProps) || props[key] !== nextProps[key]) {
logUpdate('props', key, props, nextProps)
}
}
for (let key in nextProps) {
if (!(key in props)) {
logUpdate('props', key, props, nextProps)
}
}
// shallow compare state
for (let key in state) {
if (!(key in nextState) || state[key] !== nextState[key]) {
logUpdate('state', key, state, nextState)
}
}
for (let key in nextState) {
if (!(key in state)) {
logUpdate('state', key, state, nextState)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment