I'm working on a quiz and I would like to make it so when the user clicks the correct answer it turns green if the user clicks the wrong answer it turns red.
This is what I got right now:
state = {
answerClass: false,
};
handleVoting(answer) {
if (answer == this.props.questions[this.props.questionIndex].answer) {
this.setState({
answerClass: 'green'
});
} else {
this.setState({
answerClass: 'red'
});
}
var options = [];
if (this.props.options) {
for (var i = 0; i < this.props.options.length; i++) {
options.push( < div className = {
this.state.answerClass
} >
<
img onClick = {
this.handleVoting.bind(this, this.props.options.value)
}
src = {
url
}
/></div > );
}
}
So this creates two image buttons for voting when I click on one of them I am sending a value to the handleVoting function that checks if the value clicked is the same as the answer to the question I am setting a state to the class name.
The issue with this is that if I click on the correct option both turn green and if I click on the wrong option it turns both red.
render()look like?