Skip to content

Instantly share code, notes, and snippets.

@stipsan
Last active January 28, 2018 19:18
Show Gist options
  • Save stipsan/cad35f7c88c937de3d1d4af8b978a8b0 to your computer and use it in GitHub Desktop.
Save stipsan/cad35f7c88c937de3d1d4af8b978a8b0 to your computer and use it in GitHub Desktop.
Easy react lifecycle hook debugging
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