Tell us what’s happening:
I was working around this problem but wasn’t passing one test which is
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 textTurn: N
, whereN
is the value of the counter state.
I had to look at the hint and hint says this.state.counter + 1
. I am confused because isn’t counter: prevState += 1
as I am adding +1 to what was prevState
Your code so far
class Results extends React.Component {
constructor(props) {
super(props);
}
render() {
{/* Change code below this line */}
return <h1> {this.props.fiftyFifty ? 'You Win!': 'You Lose!'}</h1>;
{/* Change code above this line */}
}
}
class GameOfChance extends React.Component {
constructor(props) {
super(props);
this.state = {
counter: 1
};
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState(prevState => {
// Complete the return statement:
return {
counter: prevState += 1
}
});
}
render() {
const expression = Math.random() >= .5; // Change this line
return (
<div>
<button onClick={this.handleClick}>Play Again</button>
{/* Change code below this line */}
{<Results fiftyFifty={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_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36
Challenge: React - Render Conditionally from Props
Link to the challenge: