Skip to content

Instantly share code, notes, and snippets.

@MorenoMdz
Last active May 1, 2018 22:14
Show Gist options
  • Save MorenoMdz/14e4e223fff444ef875cf364217a82ca to your computer and use it in GitHub Desktop.
Save MorenoMdz/14e4e223fff444ef875cf364217a82ca to your computer and use it in GitHub Desktop.
React Hidde component conditional example
function WarningBanner(props) {
if (!props.warn) {
return null;
}
return (
<div className="warning">
Warning!
</div>
);
}
class Page extends React.Component {
constructor(props) {
super(props);
this.state = {showWarning: true};
this.handleToggleClick = this.handleToggleClick.bind(this);
}
handleToggleClick() {
this.setState(prevState => ({
showWarning: !prevState.showWarning
}));
}
render() {
return (
<div>
<WarningBanner warn={this.state.showWarning} />
<button onClick={this.handleToggleClick}>
{this.state.showWarning ? 'Hide' : 'Show'}
</button>
</div>
);
}
}
ReactDOM.render(
<Page />,
document.getElementById('root')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment