I was following a tutorial on YouTube about a quiz app in react version 16. There a few variables in the state and in a function these variables are updated with the help of setState
. The values of variables are not being updated. The following in the function which is updating these variables. This function is being called from componentDidMount
function.
displayQusetions = (questions = this.state.questions, currentQuestion, nextQuestion, previousQustion) => {
let { currentQuestionIndex } = this.state;
if (!isEmpty(this.state.questions)) {
questions = this.state.questions;
currentQuestion = questions[currentQuestionIndex];
nextQuestion = questions[currentQuestionIndex + 1];
previousQustion = questions[currentQuestionIndex - 1];
const answer = currentQuestion.answer;
this.setState({
currentQuestion,
nextQuestion,
previousQustion,
answer
})
console.log(this.state.currentQuestion);
console.log(this.state.nextQuestion);
console.log(this.state.previousQustion);
console.log(this.state.answer);
}
}
I am new to react. Please help.