Skip to content

Instantly share code, notes, and snippets.

@prdxs
Created April 21, 2021 06:48
Show Gist options
  • Save prdxs/5c719e0f67825b0a179a5686e5f172c7 to your computer and use it in GitHub Desktop.
Save prdxs/5c719e0f67825b0a179a5686e5f172c7 to your computer and use it in GitHub Desktop.
class PerfClicksButton extends Component {
constructor(props) {
super(props);
this.state = {
clicks: 0,
};
}
shouldComponentUpdate(nextProps, nextState) {
const sameClassName = nextProps.className === this.props.className;
const sameStyle = nextProps.style === this.props.style;
const sameChildren = nextProps.children === this.props.children;
const sameClicks = nextState.clicks === this.state.clicks;
if (sameClassName && sameStyle && sameChildren && sameClicks) {
return false;
}
return true;
}
handleClick = () => {
this.setState(({ clicks }) => ({
clicks: clicks + 1,
}));
};
render() {
const { className, style, children } = this.props;
const { clicks } = this.state;
return (
<button
className={clsx('ClicksButton', className)}
style={style}
onClick={this.handleClick}
>
{children} {clicks}
</button>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment