I have been working on quiz app with multiple radio buttons using React along with Formik. I don't want the radio buttons to have defaultCheckedattribute. Form submit is working the way I wanted, the values of each radio buttons are shown on alert. But the radio button is not showing as selected in the browser.
import React, { useState } from 'react';
import { Formik, Form, Field } from 'formik';
import {
Button,
ButtonGroup,
Col,
Container,
FormGroup,
Input,
Label,
Row,
} from 'reactstrap';
import { Questions, initialValues } from './dummy';
import ModalConfirm from './ModalConfirm';
import './FormQuiz.css';
import ButtonGrid from './ButtonGrid';
function FormQuiz() {
const [isOpen, setIsOpen] = useState(false);
const [confirm, setConfirm] = useState(false);
const [step, setStep] = useState(1);
const [questions, setQuestions] = useState(Questions);
const toggle = (e) => setIsOpen(!isOpen);
const handleSubmit = (e) => {
e.preventDefault();
if (!confirm) {
toggle();
}
};
return (
<Container className='mt-5'>
<Row className='mx-2'>
<Col md={{ size: '6' }} className='m-auto'>
<Formik
initialValues={initialValues}
onSubmit={async (values) => {
await new Promise((r) => setTimeout(r, 500));
alert(JSON.stringify(values, null, 2));
}}
>
{({ values }) => (
<Form>
<div role='group' aria-labelledby='my-radio-group'>
{questions.map((question, questionIndex) => (
<div
key={questionIndex}
hidden={questionIndex + 1 === step ? false : true}
>
<h5 className='pl-0 my-5'>
{questionIndex + 1}
{'. '}
{question.questionText}
</h5>
{question.options.map((option, optionIndex) => (
<FormGroup
key={optionIndex}
role='group'
className='row'
>
<Field
className='form-control-sm'
type='radio'
id={`q${questionIndex + 1}o${optionIndex + 1}`}
name={`option${questionIndex + 1}`}
value={optionIndex + 1}
/>
<Label
htmlFor={`q${questionIndex + 1}o${optionIndex + 1}`}
>
{option}
</Label>
</FormGroup>
))}
<div>{values[`option${questionIndex + 1}`]}</div>
</div>
))}
</div>
<Button
color='info'
disabled={step === 1 ? true : false}
onClick={(e) => setStep(step - 1)}
className='mr-1'
>
Previous
</Button>
<Button
color='primary'
disabled={step === questions.length ? true : false}
onClick={(e) => setStep(step + 1)}
className='mr-1'
>
Next
</Button>
<Button color='warning' type='submit'>
Submit
</Button>
</Form>
)}
</Formik>
</Col>
</Row>
</Container>
);
}
export default FormQuiz;
Here is the hard coded Questions and initialValues dummy.js
export const Questions = [
{
questionText: 'HTML?',
options: [
'Hyper Tech Machine Learning',
'How To Mitigate Linda',
'How To Master Linguistics',
'Hyper Text Markup Language',
],
correctOption: 4,
},
{
questionText: 'NodeJS invented by',
options: ['Carls Linus', 'Ryan Dahl', 'Siraj Raval', 'Bill Gates'],
correctOption: 2,
}
];
let values = {};
Questions.forEach((question, index) => {
values[`option${index + 1}`] = '';
});
export let initialValues = values;
Please help me with this. And if there is any better way to implement this, let me know.