JS-React: condition ? expressionIfTrue : expressionIfFalse
If they ask "how old are you" before entering the club,this is a good training.
HTML(none)
<div id="app"></app>
CSS(sass)
html, body
height: 100%
body
background: #333
display: flex
justify-content: center
align-items: center
font-family: Helvetica Neue
h1
font-size: 2em
color: #eee
display: inline-block
a
color: white
p
margin-top: 1em
text-align: center
color: #aaa
JAVASCRIPT(babel)
const inputStyle = {
width: 235,
margin: 5
};
class CheckUserAge extends React.Component {
constructor(props) {
super(props);
this.state = {
input: "",
userAge: ""
};
this.submit = this.submit.bind(this);
this.handleChange = this.handleChange.bind(this);
}
handleChange(e) {
this.setState({
input: e.target.value,
userAge: ""
});
}
submit() {
this.setState({
userAge: this.state.input
});
}
render() {
const buttonOne = <button onClick={this.submit}>Submit</button>;
const buttonTwo = <button>You May Enter</button>;
const buttonThree = <button>You Shall Not Pass</button>;
return (
<div>
<h3>Enter Your Age to Continue</h3>
<input
style={inputStyle}
type="number"
value={this.state.input}
onChange={this.handleChange}
/>
<br />
{this.state.userAge === ""
? buttonOne
: this.state.userAge >= 18 ? buttonTwo : buttonThree}
</div>
);
}
}
React.render(<CheckUserAge />, document.getElementById("app"));
Comments