A Pen by kenzhaoyihui on CodePen.
Created
April 6, 2018 10:15
-
-
Save kenzhaoyihui/560a855d80bd86a17a300cea85b7a7e3 to your computer and use it in GitHub Desktop.
Inline If-Else with Logical && Operator
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
<div id="root"></div> |
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
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') | |
) |
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
<script src="https://unpkg.com/react/umd/react.development.js"></script> | |
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script> |
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
* { | |
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