How to create a React Radio Button

 by Robin Wieruch
 - Edit this Post

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 :

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 and an , we can keep track of the radio button's value via . 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 and to it:

import * as React from 'react';
const App = () => {
const [value, setValue] = React.useState(false);
const handleChange = () => {
setValue(!value);
};
return (
<div>
<RadioButton
label="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 now. For example, if you would give your input field some , 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>
<RadioButton
label="Cat"
value={catPerson}
onChange={handleCatChange}
/>
<RadioButton
label="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>
<RadioButton
label="Cat"
value={favorite === 'cat'}
onChange={handleCatChange}
/>
<RadioButton
label="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!

Keep reading about 

A short React tutorial by example for beginners about using a checkbox in React. First of all, a checkbox is just an HTML input field with the type of checkbox which can be rendered in React's JSX…

A short React tutorial by example for beginners about creating a select in React. First of all, a select is just an HTML select element which can be rendered in React's JSX: What may be missing is an…

The Road to React

Learn React by building real world applications. No setup configuration. No tooling. Plain React in 200+ pages of learning material. Learn React like 50.000+ readers.

Get it on Amazon.