Skip to content

Instantly share code, notes, and snippets.

@react-ram
Created October 22, 2019 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save react-ram/6b28f846f98898004f0d1443e236f33c to your computer and use it in GitHub Desktop.
Save react-ram/6b28f846f98898004f0d1443e236f33c to your computer and use it in GitHub Desktop.
basics - preventing component from rendering example ( hint: use null)
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