Skip to content

Instantly share code, notes, and snippets.

@deoxxa
Last active April 2, 2016 10:20
Show Gist options
  • Select an option

  • Save deoxxa/96796a77d90293bd47b566207bec67b7 to your computer and use it in GitHub Desktop.

Select an option

Save deoxxa/96796a77d90293bd47b566207bec67b7 to your computer and use it in GitHub Desktop.
function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || 'Component';
}
export function logComponentUpdate(WrappedComponent) {
return class Wrapped extends WrappedComponent {
static displayName = `logComponentUpdate(${getDisplayName(WrappedComponent)})`;
static WrappedComponent = WrappedComponent;
shouldComponentUpdate(nextProps, nextState) {
const changes = [];
let update = false;
for (const k in nextProps) {
if (this.props[k] !== nextProps[k]) {
if (typeof this.props[k] === 'function' && typeof nextProps[k] === 'function') {
changes.push(`props.${k}; avoidable?`);
} else {
changes.push(`props.${k}`);
update = true;
}
}
}
if (typeof nextState === 'object') {
for (const k in nextState) {
if (this.state[k] !== nextState[k]) {
changes.push(`state.${k}`);
update = true;
}
}
}
if (changes.length > 0) {
console.group(getDisplayName(WrappedComponent));
changes.forEach((l) => console.log(l));
console.groupEnd();
}
if (WrappedComponent.prototype.shouldComponentUpdate) {
return WrappedComponent.prototype.shouldComponentUpdate.call(this, nextProps, nextState);
}
return update;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment