Created
October 22, 2019 10:26
-
-
Save react-ram/6b28f846f98898004f0d1443e236f33c to your computer and use it in GitHub Desktop.
basics - preventing component from rendering example ( hint: use null)
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, { Component } from "react"; | |
export class App extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
warn: true | |
}; | |
this.handleToggle = this.handleToggle.bind(this); | |
} | |
handleToggle() { | |
this.setState(prevState => ({ warn: !prevState.warn })); | |
} | |
render() { | |
const { warn } = this.state; | |
return ( | |
<div> | |
<WarningBanner warn={warn} /> | |
<button onClick={this.handleToggle}>{warn ? "hide" : "show"}</button> | |
</div> | |
); | |
} | |
} | |
function WarningBanner(props) { | |
if (!props.warn) { | |
return null; | |
} else { | |
return <h3>Warning</h3>; | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment