Render Conditionally from Props II

Tell us what’s happening:
Describe your issue in detail here.

Hi Guys,
Can someone help me with suggestions on how to solve the query below?
///Each time the button is clicked, the counter state should be incremented by a value of 1, and a single p element should be rendered to the DOM that contains the text Turn: N , where N is the value of the counter state///.

  **Your code so far**

class Results extends React.Component {
constructor(props) {
  super(props);
}
render() {
  return (
  <h1>
  {this.props.fiftyFifty ? "You Win!" : "You Lose!"}
  </h1>
  )
};
};

class GameOfChance extends React.Component {
constructor(props) {
  super(props);
  this.state = {
    counter: 1
  };
  this.handleClick = this.handleClick.bind(this);
}
handleClick() {
  this.setState(prevState => {
         return {
      counter: prevState
    }
  });
}
render() {
  const expression = Math.random() >= .5; 
  return (
    <div>
      <button onClick={this.handleClick}>Play Again</button>
      <Results fiftyFifty={expression}/>
      <p>{'Turn: ' + this.state.counter}</p>
    </div>
  );
}
}
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36

Challenge: Render Conditionally from Props

Link to the challenge:

Hi @r_ocran ,

In your code below, the previous state of the counter has to be indicated by prevState.counter and also as per the instructions, the counter has to be incremented by 1 on click.

handleClick() {
  this.setState(prevState => {
         return {
      counter: prevState  <----  change here
    }
  });
}
1 Like

Hi manjupillai16,

Thank you for your help

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.