Tell us what’s happening:
The solution doesn’t seem to follow best practice around updating state variables (from this lesson: https://www.freecodecamp.org/learn/front-end-libraries/react/use-state-to-toggle-an-element)
The setState function should take a state argument and update state.counter, rather than update this.state.counter.
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((state) => ({
counter: state.counter + 1
}));
}
render() {
const expression = Math.random() >= .5;
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_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36
.
Challenge: Render Conditionally from Props
Link to the challenge: