Skip to content

Instantly share code, notes, and snippets.

@akatz
Created June 20, 2018 21:43
Show Gist options
  • Save akatz/a33832ad28ccb040251454a14e343c9d to your computer and use it in GitHub Desktop.
Save akatz/a33832ad28ccb040251454a14e343c9d to your computer and use it in GitHub Desktop.
declare interface ObjectConstructor {
entries<T>(o: { [s: string]: T } | ArrayLike<T>): [string, T][];
entries(o: {}): [string, any][];
}
interface IProps {
[index: string]: {};
}
public componentWillReceiveProps(nextProps: IProps) {
propDiff(this.props, nextProps);
}
type PropTuple = [string, Object];
const propDiff = (currentProps: IProps, nextProps: IProps): void {
const existingProps: Array<PropTuple> = Object.entries(currentProps);
const added = existingProps.filter(([propName, props]: PropTuple) => {
if (nextProps[propName] === undefined) { return true; }
if (nextProps[propName] !== props) {
// tslint:disable-next-line:no-console
console.log(`${propName}
- ${JSON.stringify(props)}
+ ${JSON.stringify(nextProps[propName])}`);
}
return false;
});
// tslint:disable-next-line:no-console
added.forEach(([propName, props]: PropTuple) => console.log(`${propName}
+ ${JSON.stringify(props)}`));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment