Skip to content

Commit fd74b7c

Browse files
Fix option selection
1 parent 1a44a09 commit fd74b7c

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

components/QuizForm.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FC, useState } from "react";
1+
import { type FC, useState } from "react";
22
import { useForm, FieldValues } from "react-hook-form";
33
import { Question } from "./types";
44
import Image from "next/image";
@@ -22,7 +22,7 @@ const QuizForm: FC<Props> = ({
2222
totalQuestions,
2323
link,
2424
}) => {
25-
const { register, handleSubmit, reset } = useForm();
25+
const { register, handleSubmit, reset, watch } = useForm();
2626
const [showCorrectAnswer, setShowCorrectAnswer] = useState<boolean>(false);
2727
const [lastIndex, setLastIndex] = useState<number>(1);
2828
const [canGoBack, setCanGoBack] = useState<boolean>(false);
@@ -55,6 +55,7 @@ const QuizForm: FC<Props> = ({
5555

5656
if (isLoading) return <p>Loading...</p>;
5757
const { question, options, images } = questionSet!;
58+
const watchInput = watch(`options.${currentQuestionIndex}`);
5859

5960
const noOfAnswers = options.filter((el) => el.isAnswer === true).length;
6061
return (
@@ -213,6 +214,10 @@ const QuizForm: FC<Props> = ({
213214
disabled={currentQuestionIndex < lastIndex}
214215
onClick={() => {
215216
setShowCorrectAnswer(false);
217+
setSavedAnswers((prev) => ({
218+
...prev,
219+
[currentQuestionIndex]: watchInput,
220+
}));
216221
if (currentQuestionIndex === totalQuestions) {
217222
handleNextQuestion(1);
218223
setLastIndex(1);

0 commit comments

Comments
 (0)