Skip to content

Instantly share code, notes, and snippets.

@mmolucio
Forked from NoamELB/ShouldNotUpdate.js
Created January 28, 2019 01:31
Show Gist options
  • Save mmolucio/b38203cd51fe672a2376b92e2f578985 to your computer and use it in GitHub Desktop.
Save mmolucio/b38203cd51fe672a2376b92e2f578985 to your computer and use it in GitHub Desktop.
component that should not update
class ShouldNotUpdate extends React.Component {
constructor(props) {
super(props);
this.counter = 0;
}
shouldComponentUpdate(nextProps, nextState) {
return this.props.children !== nextProps.children;
}
render() {
return `I should be rendered only 1 time. actual times rendered: ${++this.counter}`;
}
}
// In parent render: <ShouldNotUpdate><div/></ShouldNotUpdate>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment