A short React tutorial by example for beginners about using a radio button in React. First of all, a radio button is just an HTML input field with the type of radio which can be rendered in React's JSX:
import * as React from 'react';const App = () => {return (<div><input type="radio" /></div>);};export default App;
What may be missing is an associated label to signal the user what value is changed with this radio button:
import * as React from 'react';const App = () => {return (<div><label><input type="radio" /> Cat</label></div>);};export default App;
In your browser, this radio button can already change its checked state. However, this is just the radio button's internal HTML state which isn't controlled by React yet. Let's change this by transforming this radio button from being uncontrolled to controlled:
import * as React from 'react';const App = () => {const [value, setValue] = React.useState(false);const handleChange = () => {setValue(!value);};return (<div><label><input type="radio" checked={value} onChange={handleChange} />Cat</label></div>);};export default App;
By using React's useState Hook and an event handler, we can keep track of the radio button's value via React's state. Next we may want to create a Radio Button component which can be used more than once throughout a React project. Therefore, we will extract it as a new function component and pass the necessary props to it:
import * as React from 'react';const App = () => {const [value, setValue] = React.useState(false);const handleChange = () => {setValue(!value);};return (<div><RadioButtonlabel="Cat"value={value}onChange={handleChange}/></div>);};const RadioButton = ({ label, value, onChange }) => {return (<label><input type="radio" checked={value} onChange={onChange} />{label}</label>);};export default App;
Our Radio Button component is a reusable component now. For example, if you would give your input field some CSS style in React, every Radio Button component which is used in your React project would use the same style.
If you would want to create a radio button group now, you could just use multiple Radio Button components side by side and maybe style them with some border and some alignment, so that a user perceives them as a group of radio buttons:
import * as React from 'react';const App = () => {const [catPerson, setCatPerson] = React.useState(false);const [dogPerson, setDogPerson] = React.useState(false);const handleCatChange = () => {setCatPerson(!catPerson);};const handleDogChange = () => {setDogPerson(!dogPerson);};return (<div><RadioButtonlabel="Cat"value={catPerson}onChange={handleCatChange}/><RadioButtonlabel="Dog"value={dogPerson}onChange={handleDogChange}/></div>);};export default App;
If you want to enforce that only one radio button can be checked for the radio button group, you need to change it the following way:
import * as React from 'react';const App = () => {const [favorite, setFavorite] = React.useState('dog');const handleCatChange = () => {setFavorite('cat');};const handleDogChange = () => {setFavorite('dog');};return (<div><RadioButtonlabel="Cat"value={favorite === 'cat'}onChange={handleCatChange}/><RadioButtonlabel="Dog"value={favorite === 'dog'}onChange={handleDogChange}/></div>);};export default App;
That's is it for creating a Radio Button component in React. If you are a beginner in React, I hope this tutorial helped you to understand some concepts and patterns!