Skip to content

Instantly share code, notes, and snippets.

@mahdiyazdani
Created September 25, 2018 21:41
Show Gist options
  • Save mahdiyazdani/5eafc5cb795ca518fc2c5b60a686d461 to your computer and use it in GitHub Desktop.
Save mahdiyazdani/5eafc5cb795ca518fc2c5b60a686d461 to your computer and use it in GitHub Desktop.
shouldComponentUpdate
import React from 'react';
export class Example extends React.Component {
constructor(props) {
super(props);
this.state = { subtext: 'Put me in an <h2> please.' };
}
shouldComponentUpdate(nextProps, nextState) {
if ((this.props.text == nextProps.text) &&
(this.state.subtext == nextState.subtext)) {
alert("Props and state haven't changed, so I'm not gonna update!");
return false;
} else {
alert("Okay fine I will update.")
return true;
}
}
render() {
return (
<div>
<h1>{this.props.text}</h1>
<h2>{this.state.subtext}</h2>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment