Skip to content

Instantly share code, notes, and snippets.

@JoaoCnh
Created January 31, 2018 00:32
Show Gist options
  • Save JoaoCnh/18000be61b7cb1849b9251f975498320 to your computer and use it in GitHub Desktop.
Save JoaoCnh/18000be61b7cb1849b9251f975498320 to your computer and use it in GitHub Desktop.
Avatar shouldComponentUpdate
import React from "react";
export default class Avatar extends React.Component {
state = {
open: false,
};
shouldComponentUpdate(nextProps, nextState) {
const photoChanged = this.props.avatarUrl !== nextProps.avatarUrl;
const optionsWereToggled = this.state.open !== nextState.open;
return photoChanged || optionsWereToggled;
}
render() {
return (
<div>
<img src={this.props.avatarUrl} />
{this.state.open && (<Dropdown />)}
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment