DEV Community

Randy Rivera
Randy Rivera

Posted on

React: Use a Ternary Expression for Conditional Rendering

  • The ternary operator is often utilized as a shortcut for if/else statements in JavaScript.
  • Ternary expressions can be a great alternative if you want to implement conditional logic within your JSX. Ternary operator has three parts, but you can combine several ternary expressions together. Here's the basic syntax:
condition ? expressionIfTrue : expressionIfFalse;
Enter fullscreen mode Exit fullscreen mode
  • In this lesson FreeCodeCamp wants us to set up a ternary expression that implements the following logic: when the page first loads, render the submit button, buttonOne, to the page. Then, when a user enters their age and clicks the button, render a different button based on the age. If a user enters a number less than 18, render buttonThree. If a user enters a number greater than or equal to 18, render buttonTwo.
  • First we have to initialize the state of CheckUserAge with input and userAge both set to values of an empty string.
const inputStyle = {
  width: 235,
  margin: 5
};

class CheckUserAge extends React.Component {
  constructor(props) {
    super(props);
    // Change code below this line

    // Change code above this line
    this.submit = this.submit.bind(this);
    this.handleChange = this.handleChange.bind(this);
  }
  handleChange(e) {
    this.setState({
      input: e.target.value,
      userAge: ''
    });
  }
  submit() {
    this.setState(state => ({
      userAge: 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 />
        {/* Change code below this line */}

        {/* Change code above this line */}
      </div>
    );
  }
}
Enter fullscreen mode Exit fullscreen mode
  • Answer:
class CheckUserAge extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      input: '',
      userAge: ''
}
Enter fullscreen mode Exit fullscreen mode
  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
         ? buttonThree
         : buttonTwo
        }  
      </div>
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)