In React, you can work with radio buttons by using the <input type="radio" /> element and managing their selection through React state using the useState Hook. Each radio button in a group should share the same name attribute, ensuring only one can be selected at a time.
How to use radio buttons in React JS
Key takeaways:
Radio buttons in React.js enable users to select a single option from a group, making them an essential element in form design.
Assign the same
nameattribute to all radio buttons in a group to ensure only one option can be selected at a time.Use the
defaultCheckedattribute to set a default radio button selection when the form loads.The
disabledattribute makes a radio button unselectable, helping restrict choices or indicate unavailable options.Use React's
useStateHook to track and manage the selected radio button dynamically.Use the
onValueChangefunction to update the selected value when a radio button is clicked.Use the
formSubmitfunction to handle form submissions, prevent page reloads, and capture user selections.
React.js is a powerful JavaScript framework used for building the user interfaces of single-page applications. The radio button is a significant element used in web application forms that allow users to select a specific option from a group. In this Answer, we’ll look into how to use radio buttons in React.js, including setting default values, handling selections, and disabling options.
Note: If you need to select multiple options, consider using checkboxes instead.
The syntax for radio buttons in React JS
The syntax for the radio button in React is the following:
<input type="radio" name="name1" value="value1"/>
where:
type: Set this attribute toradioto create a radio button.name: Usenameto define a group of radio buttons. A group is a collection of radio buttons with the samename, allowing only one option in the group to be selected at a time.value: This sets the value that will be retrieved once the form is submitted.
In the following sections, we’ll cover how to use the defaultChecked and disabled attributes in React to select and disable radio buttons by default.
The defaultChecked attribute
React.js allows us to mark a radio button as checked by default using the defaultChecked attribute. This example demonstrates how to set up a default radio button selection in React.
In the above App.js file:
Line 8: The
defaultCheckedattribute on thePizzaoption makes it the default selection when the form loads. Other options remain unselected.
The disabled attribute
React JS also allows us to disable a radio button, making it unclickable using the disabled attribute. When disabled is set to true, the radio button becomes inactive. We will see the working of the disabled attribute in the following example.
In the above App.js file:
Line 8: The
Pizzaoption is unselectable because thedisabledattribute is applied to theinputelement.
Handling radio button selection in React
To give a clear idea of using radio buttons in React, here’s an example of a form that uses radio buttons to capture a user’s selected color. We’ll use useState to manage the selected radio button in React.
Code explanation
In the above App.js file:
Line 8: We create the state variable
selectedOptionto store the user’s selection.Line 11–14: We create
onValueChangefunction that updates the value ofselectedOptionwhen a radio button is selected.Line 17–26: We create
formSubmitfunction that prevents the page from reloading. It also logs the value ofselectedOptionand shows analertmessage of theselectedOptionon submission of the form.Line 30: We create a form with the
onSubmitfunction. The function runs once the submit button is clicked on line 77.Line 35–40: We create a radio button for
Blue, controlled by thecheckedattribute.Line 47–52: Similar setup for the
Greenradio button.Line 59–64: Similar setup for the
Pinkradio button.Line 71–73: We create a
divand write the textSelected option is:along with the variableselectedOption.Line 77–79: We create a button of type
submit. Once we click the button, the form gets submitted.
Knowledge test
Let’s attempt a short quiz to assess your understanding.
In a React application, how can we ensure that only one radio button is selected at a time in a group of radio buttons?
By setting the checked attribute for each radio button to true
By assigning the same name attribute to all radio buttons in the group
By using the defaultChecked attribute for each radio button
By applying the disabled attribute to all but one radio button
Conclusion
Radio buttons in React.js provide functionality to choose one option from multiple choices. We can add a radio button in React by setting the input tag’s type attribute to radio. Use disabled to make a radio button unclickable, and defaultChecked to set a default selection when the page first loads. By handling state with useState, we can easily manage and display the selected radio button in React, making forms more interactive and user-friendly.
If you’re looking to dive deeper into React, consider reading the detailed blog, “Five best practices for React developers.” This guide provides foundational insights that will enhance your React skills and set you up for greater success in building interactive and efficient applications.
Frequently asked questions
Haven’t found what you were looking for? Contact Us
How to work with radio buttons in React.js
How to reset radio button in React.js
How to retrieve value from the radio button
Free Resources