Hi everybody.
I hope everyone is fine. I am back here because I have a little problem with my componentDidUpdate and, as I am fairly new to React, I don’t understand why (which is a bit annoying for me because I want to understand why.
)
First here is my componentDidUpdate with the code related to it:
this.state = {
answerIsCorrect: false,
answerIsWrong: false,
};
this.resetAnswer = this.resetAnswer.bind(this);
}
isAnswerCorrect(valeurReponse) {
if(valeurReponse === this.props.evalStatus.test.reponseCorrecte) {
this.setState({answerIsCorrect: true})
console.log("Bien joué")
}
else {
this.setState({
answerIsWrong: true});
console.log("What ?!")
}
}
resetAnswer() {
if(this.state.answerIsWrong === true) {
this.setState({
answerIsWrong: false});
} else if (this.state.answerIsCorrect === true) {
this.setState({
answerIsCorrect: false});
}
console.log("Ok", this.state.answerIsCorrect, this.state.answerIsWrong)
}
componentDidUpdate(prevProps, prevState) {
this.timer = setTimeout((answerIsWrong, answerIsCorrect) => this.resetAnswer(answerIsWrong, answerIsCorrect), 3000);
}
componentWillUnmount() {
clearTimeout(this.timer);
}
render () {
const{evalStatus, selectTest} = this.props;
const displayReponses = evalStatus.test.reponses.map(reponse =>
<MDBBtn className='response' key={reponse.id} onClick={() => {
this.isAnswerCorrect(reponse.reponse); this.chargeReponseClick(reponse.reponse)}}>{reponse.reponse} </MDBBtn>)
And here is a picture of my console.log :
So my question is why my componentDidUpdate is rendering two times each time it is working ?
In fact, I am doing a language quiz app and if the user clicks on one of the wrong answers it displays a “X” and if it’s a good answer it displays a “O”. So, I need to make it disappear after a few secondes and if the user clicks on the same button a second time (or on another answer buttons) the “X” or “O” will display again and have to disappear again, etc. So I think I need to use componentDidUpdate in this case.
So how can I solve this issue? Where do I have to start?
I would be very glad if some kind people could give me some idea and thanks everyone for taking time to read me.
Edit : Problem solved.