Last active
January 28, 2018 19:18
-
-
Save stipsan/cad35f7c88c937de3d1d4af8b978a8b0 to your computer and use it in GitHub Desktop.
Easy react lifecycle hook debugging
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
import {Component} from 'react' | |
import {diff} from 'deep-object-diff' | |
const flattenObject = function(ob) { | |
const toReturn = {} | |
for (const i in ob) { | |
if (!ob.hasOwnProperty(i)) continue | |
if (typeof ob[i] == 'object') { | |
const flatObject = flattenObject(ob[i]) | |
for (const x in flatObject) { | |
if (!flatObject.hasOwnProperty(x)) continue | |
toReturn[i + '.' + x] = flatObject[x] | |
} | |
} else { | |
toReturn[i] = ob[i] | |
} | |
} | |
return toReturn | |
} | |
const compareProps = (prevProps, nextProps) => { | |
const props = [prevProps, nextProps].map(flattenObject) | |
const changed = Object.keys(diff(...props)) | |
changed.length > 0 && console.table(props, changed) | |
} | |
class extends Component { | |
componentWillReceiveProps(nextProps) { | |
console.group('componentWillReceiveProps') | |
compareProps(this.props, nextProps) | |
console.groupEnd('componentWillReceiveProps') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment