Skip to content

Instantly share code, notes, and snippets.

@kenzhaoyihui
Created April 6, 2018 10:15
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 kenzhaoyihui/560a855d80bd86a17a300cea85b7a7e3 to your computer and use it in GitHub Desktop.
Save kenzhaoyihui/560a855d80bd86a17a300cea85b7a7e3 to your computer and use it in GitHub Desktop.
Inline If-Else with Logical && Operator
<div id="root"></div>
function WarningBanner(props) {
if(!props.warn) {
return null;
}else{
return (
<div className="warning">
Warning!!!
</div>
);
}
}
class Page extends React.Component {
constructor(props){
super(props);
this.state = {showWarning: true};
this.handClick = this.handClick.bind(this);
}
handClick() {
this.setState(preState => ({
showWarning : !preState.showWarning
}));
}
render () {
return (
<div>
<WarningBanner warn={this.state.showWarning} />
<button onClick={this.handClick}>
{this.state.showWarning ? 'Hide': "Show"}
</button>
</div>
);
}
}
ReactDOM.render(
<Page />,
document.getElementById('root')
)
<script src="https://unpkg.com/react/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script>
* {
font-family: sans-serif;
margin: 0;
}
button {
height: 40px;
width: 200px;
}
.warning {
background-color: red;
text-align: center;
width: 100%;
padding: 10px;
font-size: 50px;
color: orange;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment