Last active
February 1, 2017 16:39
-
-
Save poislagarde/551870e36fd549d07d620348b23a6824 to your computer and use it in GitHub Desktop.
React debug component update
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
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