React.memo
can help you control what components re-render. It's like a PureComponent
in the Class syntax, only this is for function components.
Created
April 19, 2023 15:19
-
-
Save OluwatobilobaGp/55e2c107940f49b3ca01af45b34286e3 to your computer and use it in GitHub Desktop.
React Memo Example
This file contains hidden or 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
<div id="root"></div> |
This file contains hidden or 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
const ChildOne = React.memo(props => { | |
console.log("Rerendering Child One"); | |
return( | |
<div className="box"> | |
<h2>Hello! {props.name}</h2> | |
<p>This component won't re-render, check your console.</p> | |
</div> | |
) | |
}); | |
const ChildTwo = (props) => { | |
console.log('Rerendering Child Two') | |
return ( | |
<div className="box"> | |
<h2>Hello! {props.name}</h2> | |
<p>This component will render re-render, check your console.</p> | |
</div> | |
) | |
} | |
class App extends React.Component { | |
state = { | |
value: 1, | |
name: "Chris" | |
}; | |
handleClick = () => { | |
this.setState({ | |
value: this.state.value + 1 | |
}); | |
}; | |
render() { | |
return ( | |
<React.Fragment> | |
<div className="box"> | |
<div>{this.state.value}</div> | |
<button onClick={this.handleClick}>+</button> | |
</div> | |
<ChildOne name={this.state.name} /> | |
<ChildTwo name={this.state.name} /> | |
</React.Fragment> | |
); | |
} | |
} | |
ReactDOM.render(<App />, | |
document.getElementById("root")) |
This file contains hidden or 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
<script src="https://unpkg.com/[email protected]/umd/react.production.min.js"></script> | |
<script src="https://unpkg.com/[email protected]/umd/react-dom.production.min.js"></script> |
This file contains hidden or 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
body { | |
height: 100vh; | |
margin: 0; | |
display: grid; | |
place-items: center; | |
} | |
.box { | |
width: 300px; | |
h1 { | |
font-size: 20px; | |
margin: 0 0 1rem 0; | |
} | |
} |
This file contains hidden or 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
<link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.4/css/bulma.min.css" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I find it interesting