I am not able to set the defaultChecked value in Radio button ReactJS.
default value is depends on API fetched data.
Below is my useState:
const [edit_data, setEditData] = useState([]);
API Call
useEffect(() => {
(async () => {
let url = window.location.href
var array_fragment = url.toString().split("/")
var content_id = array_fragment[array_fragment.length - 1]
const result = await axios(`${process.env.REACT_APP_API_URL}/edit_content/${content_id}`);
setEditData(result.data);
})();
}, []);
I have tried this set last value(Other) radio button but actual want to set Lecture radio button. edit_data.content_type return the "Lecture" value.
defaultChecked={edit_data.content_type === 'Value' ? "true" : "false"}
React radio button code:
<fieldset>
<Form.Label>Content Type</Form.Label>
<Row className="d-flex flex-wrap flex-md-nowrap align-items-center py-4" value={contenttype} onChange={(e) => setContentType(e.target.value)}>
<Col className="d-block mb-4 mb-md-0">
<Form.Check
defaultChecked={edit_data.content_type === 'Lecture' ? "true" : "false"}
type="radio"
defaultValue="option1"
label="Lecture"
name="x"
value="Lecture"
id="radio1"
htmlFor="radio1"
/>
</Col>
<Col className="d-block mb-4 mb-md-0">
<Form.Check
defaultChecked={edit_data.content_type === 'Seminar' ? "true" : "false"}
type="radio"
defaultValue="option2"
label="Seminar"
name="x"
value="Seminar"
id="radio2"
htmlFor="radio2"
/>
</Col>
<Col className="d-block mb-4 mb-md-0">
<Form.Check
defaultChecked={edit_data.content_type === 'Study Material' ? "true" : "false"}
type="radio"
defaultValue="option3"
label="Study Material"
name="x"
value="Study Material"
id="radio3"
htmlFor="radio3"
/>
</Col>
<Col className="d-block mb-4 mb-md-0">
<Form.Check
defaultChecked={edit_data.content_type === 'Other' ? "true" : "false"}
type="radio"
defaultValue="option4"
label="Other"
name="x"
value="Other"
id="radio4"
htmlFor="radio4"
/>
</Col>
</Row>
</fieldset>
await axios(`${process.env.REACT_APP_API_URL}/edit_content/${content_id}`);?edit_dataas an array so it won't have acontent_typeproperty on the initial render.