Created
September 25, 2018 21:41
-
-
Save mahdiyazdani/5eafc5cb795ca518fc2c5b60a686d461 to your computer and use it in GitHub Desktop.
shouldComponentUpdate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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