Render Conditionally from Props

Tell us what’s happening:

I get this error message :

When the GameOfChance component is first mounted to the DOM and each time the button is clicked thereafter, a single h1 element should be returned that randomly renders either You Win! or You Lose!.

Your code so far


class Results extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <h1>
      {
       this.fiftyFifty ? "You win!" : "You lose!"
        /* change code here */
      }
      </h1>
    )
  };
};

class GameOfChance extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      counter: 1
    }
    this.handleClick = this.handleClick.bind(this);
  }
  handleClick() {
    this.setState({
      counter: this.state.counter + 1 // change code here
    });
  }
  render() {
    let expression = Math.random() > .5 ; // change code here
    return (
      <div>
        <button onClick={this.handleClick}>Play Again</button>
        { /* change code below this line */ }
        <Results fiftyFifty = {this.expression} />
        { /* change code above this line */ }
        <p>{'Turn: ' + this.state.counter}</p>
      </div>
    );
  }
};

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/react/render-conditionally-from-props

1 Like

You have two problems.

In your Results render you have:

      {
       this.fiftyFifty ? "You win!" : "You lose!"
        /* change code here */
      }

But remember that fiftyFifty is being passed in on the this.props object, not directly to this.

And in your GameOfChance render, you have

<Results fiftyFifty = {this.expression} />

But expression is just a local variable to the render function and not a member of the class GameOfChance so there is no need for this.

3 Likes

Ah i get it. Thank you.