-
hy iam sorry for my bad english, i create a project, in this project i create quiz form with react and react hook form // import third library // regular quiz validation schema const CreateRegularQuiz = () => { // array of question config const navigate = useNavigate(); const onSubmit = async (data) => {
}; const onError = (err) => console.log(err); return ( {/* navbar */} {user && ( {user.email} Logout )}
); const NestedArray = ({ questionIndex, control, register, errors }) => { return ( {answerField.map((answer, answerIndex) => ( <> {/* answer text /} <input type="text" className="px-4 py-2 border border-gray-400 rounded" placeholder={ Option ${answerIndex + 1} }{...register( question.${questionIndex}.answer.${answerIndex} )}/> {/ input radio untuk correct answer /} <input type="radio" value={answerIndex} {...register( question.${questionIndex}.correctAnswer )}/> {/ button untuk mengurangi option /} {answerField.length > 1 && ( <button onClick={() => RemoveAnswer(answerIndex)} className="px-4 py-2 rounded border border-red-500 bg-red-500 text-white hover:bg-red-600"> - option )} {/ error ketika salah tidak ada jawaban benar yang dipilih /} {answerIndex === 0 && ( {errors.question?.[questionIndex]?.correctAnswer?.message} )} {/ error ketika input text answer kosong */} {errors.question?.[questionIndex]?.answer?.[answerIndex]?.message}
); export default CreateRegularQuiz; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
This is a lot of code without reproduction steps and a codesandbox 😔 One thing I can point you towards is check if it happens when you click any of the other non-submit buttons. HTML buttons behave as |
Beta Was this translation helpful? Give feedback.
Yup, so, like I said, it's because the default HTML buttons type is
submit
insideform
element , which triggers the form submission in your form.Set non-submit buttons
type="button"
to fix it.Here's a working example codesandbox.