Hi everyone. Can you tell what’s wrong with my code?
Other than this.setState(() => {counter: this.state.counter++} // Change this line
);
the rest is like the answer, but there’s something I’m not doing right… what is it? and would you use the arrow function for counter or what it’s in the answer?
Thanks.
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(() => {counter: this.state.counter++} // Change this line
);
console.log(this.state)
}
render() {
const expression = Math.random() >= .5 ? true : false; // 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36
.
Challenge: Render Conditionally from Props
Link to the challenge: