Skip to content

Instantly share code, notes, and snippets.

@paoloiulita
Created March 14, 2019 09:22
Show Gist options
  • Save paoloiulita/6b01f8734acda35f53892addd47bc5a2 to your computer and use it in GitHub Desktop.
Save paoloiulita/6b01f8734acda35f53892addd47bc5a2 to your computer and use it in GitHub Desktop.
componentDidUpdate(prevProps, prevState) {
Object.keys(prevProps)
.filter(key => (prevProps[key] !== this.props[key]))
.map(key => {
if (typeof prevProps[key] === 'object' || typeof this.props[key] === 'object') {
console.log(`changed prop: ${key}`);
console.log(prevProps[key]);
console.log(this.props[key]);
} else {
console.log(`changed prop: ${key} from ${prevProps[key]} to ${this.props[key]}`);
}
});
Object.keys(prevState)
.filter(key => (prevState[key] !== this.state[key]))
.map(key => {
if (typeof prevState[key] === 'object' || typeof this.state[key] === 'object') {
console.log(`changed state: ${key}`);
console.log(prevState[key]);
console.log(this.state[key]);
} else {
console.log(`changed state: ${key} from ${prevState[key]} to ${this.state[key]}`);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment