Created
May 23, 2019 03:31
-
-
Save pc035860/dda388445bb3550a5fb66aaad5822e62 to your computer and use it in GitHub Desktop.
HoC for debug component update
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
/* eslint no-console: 0 */ | |
import _ from 'lodash'; | |
import { diff } from 'deep-object-diff'; | |
import { shouldUpdate } from 'recompose'; | |
export default function debugUpdate(groupLabel, collapsed = true) { | |
if (typeof groupLabel === 'undefined') { | |
throw new Error('Should provide `groupLabel`.'); | |
} | |
return shouldUpdate((props, nextProps) => { | |
if (process.env.NODE_ENV !== 'production') { | |
let count = 0; | |
_.each(props, (v, k) => { | |
if (v !== nextProps[k]) { | |
if (count === 0) { | |
const mainLabel = | |
typeof groupLabel === 'function' ? groupLabel(props) : groupLabel; | |
console[collapsed ? 'groupCollapsed' : 'group']( | |
`[debugUpdate] ${mainLabel}` | |
); | |
} | |
console.groupCollapsed(`"${k}"`); | |
console.log('%c current ', 'background: #B8E5EC', v); | |
console.log('%c next ', 'background: #FCD4B3', nextProps[k]); | |
console.log( | |
'%c diff ', | |
'color: white; background: #EE4F33', | |
diff(v, nextProps[k]) | |
); | |
console.groupEnd(); | |
count++; | |
} | |
}); | |
if (count > 0) { | |
console.groupEnd(); | |
} | |
} | |
return true; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment