1

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>
3
  • 1
    What is the response from await axios(`${process.env.REACT_APP_API_URL}/edit_content/${content_id}`);? Commented Dec 27, 2021 at 6:25
  • @HanchenJiang json response is { added_on: null, category: [1], content_file: "/media/media/download_QpgAjoK.jpg", content_type: "Lecture", description: "nil", expiration: "2021-12-09", id: 3, publish_now: true, schedule_release: "2021-12-23", school_profile: 4, show: 1, shows_name: "nil", sponsor_link: "nil", status: false, subtitle: "niln", tag: (2) [1, 2], title: "nil", topic: [1], user: 7 } Commented Dec 27, 2021 at 6:29
  • You've initialised edit_data as an array so it won't have a content_type property on the initial render. Commented Dec 27, 2021 at 6:29

1 Answer 1

1

You have replace "true" with true and "false" with false. Strings are treated as true. Empty strings are treated as false.

defaultChecked={edit_data.content_type === 'Value' ? true : false}

You have to add the checked attribute to the radio as mentioned below and also change the state on onClick event if you want the radio button to toggle.

<Form.Check
defaultChecked={edit_data.content_type === 'Study Material' ? true : false}
checked={edit_data.content_type === 'Study Material' ? true : false}
type="radio"
defaultValue="option3"
label="Study Material"
name="x"
value="Study Material"
id="radio3"
htmlFor="radio3"
/>

Check the codesandbox which demonstrates the solution.

Sign up to request clarification or add additional context in comments.

9 Comments

tried but not working
Are there any errors in the console?
No not showing any errors related to defaultChecked
Check the edited response please
I have changed defaultChecked={edit_data.content_type === 'Study Material' ? true : false} checked={edit_data.content_type === 'Study Material' ? true : false} in code its set the defaultChecked redio button but after that i am not able change redio button
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.