I have a quiz there are 4 questions those are dynamic coming from API.
Now when I click on radio button for first question then move to next question again click on previous button then radio is unselecting. Why this happening?
Thanks for your efforts!
3
Answers
To keep the selected answer when the user clicks on the previous button, you need to store the user’s selection for each question in the state. Currently, you are storing the selected answer index in the selectedAnswerIndex state variable, but you are resetting it to null when the next button is clicked. Instead, you can store the selected answer for each question in an array and update it whenever the user selects an answer. Here’s how you can modify your code to achieve this:
1.Add a new state variable called selectedAnswers to store the user’s answers for each question. Initialize it with an empty array:
2.Modify the handleQuestionInput function to update the selectedAnswers array whenever the user selects an answer:
In this function, we first extract the index of the question from the name attribute of the selected radio button. We then update the selectedAnswers array at that index with the user’s answer. Finally, we update the selectedAnswerIndex state with the user’s answer.
3.Modify the prevQuestion function to set the current question and selected answer to the previous values stored in the state:
In this function, we first update the question state to the previous question. We then get the index of the previous question and use it to retrieve the selected answer from the selectedAnswers array. We then update the selectedAnswerIndex state with the previous selected answer.
With these modifications, the user’s selected answer for each question will be stored in the selectedAnswers array and will persist when the user clicks on the previous button.
Modify this to hold the state of the selected answer,
NOTE: having correct answers storing the indices can make it easy to decide correct/incorrect answer easily (you can follow your approach also, you have to use string matching instead)
onChange
of checkbox/radio, set the selected answers’ indicesYou need to add the selections to the state.
I have taken some liberty to refactor your code to have the questions in the state, this would make is easier to maintain and update
Explanation on the changes:
Add selected to the quiz data
I have added
selected: []
to each question in the quiz object.Use the quiz in the state
I have moved your quiz to the state itself
This will let us better control the form
Control the current question in view
We can use useMemo to always have the current question available for us to read
I have moved all logic relying on the question to use this memo
Set different update methods
I have updated the code to have update method per type:
“javascript
const updateSelection = (e, index) => {
setQuizQuestions((quizQuestions) => {
quizQuestions[question].question.selected[index] =e?.target?.checked? e.target.value: null
return […quizQuestions];
});
};
const updateDropDown = (val, index) => {
setQuizQuestions((quizQuestions) => {
if (val) {
quizQuestions[question].question.selected[index] = val
} else {
quizQuestions[question].question.selected[index] = null
}
return […quizQuestions];
});
};
const setSelection = (e) => {
setQuizQuestions((quizQuestions) => {
quizQuestions[question].question.selected = [e.target.value];
return […quizQuestions];
});
};
And in the Select I have used
And now the form is controlled, synced and up to date
What’s next?
Now you can put some efforts in cleaning up the code and making it more robust
Good luck!