Introduction
Radio buttons are ideal for allowing users to select one option from a group. But what if you also want to allow users to deselect their choice? That’s where a custom DeselectableRadioGroup component becomes useful.
In this article, you’ll learn how to build a DeselectableRadioGroup using React and Material UI, including full setup, usage, and code breakdown.
🛠️ Step 1. Project Setup
If you haven’t already created a React project, run:
npx create-react-app react-deselectable-radio
cd react-deselectable-radio

Image 1. Create React App tool.
- The command npx create-react-app react-deselectable-radio is used to create a new React application,react-deselectable-radio, using the Create React App tool.
- It creates a ready-to-use React project with all the necessary files and configurations.
- After the project is created, cd react-deselectable-radio changes the current directory to the new project folder so you can start working inside it.
- Finally, npm start launches the development server and opens the app in your default browser, allowing you to see your React application running locally.
Then install the necessary dependencies:
💿Install the Material UI
Go to the Material-UI website and get some instructions. There are several types of versions (V4, V5, V6, and V7) in Material-UI.

Image 2. Types of versions(V4, V5, V6, and V7) in Material-UI.
➕ Now I'm going to install the Mui and Style.
npm install @mui/material @emotion/react @emotion/styled @mui/icons-material react-icons
📦 Step 2. DeselectableRadioGroup Component Code
Create a new file at: src/Component/DeselectableRadioGroup.jsx
import React, { useState, useEffect } from 'react';
import { ToggleButton, ToggleButtonGroup } from '@mui/material';
import RadioButtonCheckedRounded from '@mui/icons-material/RadioButtonCheckedRounded';
import RadioButtonUncheckedRounded from '@mui/icons-material/RadioButtonUncheckedRounded';
import { Button, Tooltip } from '@mui/material';
import { FaUndo } from 'react-icons/fa';
const DeselectableRadioGroup = ({
classes,
ariaLabel,
name,
options,
value,
onChange,
title,
refreshDisabled,
refreshOnClick,
disabled,
}) => {
let initialSelectedValue = '';
if (value === 1 || value === 0) {
initialSelectedValue = true;
} else {
initialSelectedValue = value;
}
const [selectedValue, setSelectedValue] = useState(initialSelectedValue);
useEffect(() => {
if (initialSelectedValue === undefined || initialSelectedValue === null) {
setSelectedValue(null);
} else {
setSelectedValue(initialSelectedValue);
}
}, [initialSelectedValue]);
const handleChange = (event, newValue) => {
setSelectedValue(newValue);
onChange({ target: { name, value: newValue } });
};
return (
<>
<label className={classes?.globalCreateLabel} htmlFor={title}>
{title}
</label>
<span> :</span>
<div>
<ToggleButtonGroup
color="primary"
value={selectedValue}
exclusive
onChange={handleChange}
aria-label={ariaLabel}
>
{options.map((option) => (
<ToggleButton
key={option.value}
value={option.value}
disabled={disabled}
aria-label={option.label}
>
{selectedValue === option.value ? (
<RadioButtonCheckedRounded fontSize="small" />
) : (
<RadioButtonUncheckedRounded fontSize="small" />
)}
{option.label}
</ToggleButton>
))}
</ToggleButtonGroup>
{!refreshDisabled && (
<Tooltip title="Reset to default value">
<Button
aria-label={name}
id={name}
name={name}
disabled={refreshDisabled}
onClick={refreshOnClick}
className={classes?.resetButton}
>
<FaUndo style={{ fontSize: '10px' }} />
</Button>
</Tooltip>
)}
</div>
</>
);
};
export default DeselectableRadioGroup;
➕ Import: Special need to import this
import { ToggleButton, ToggleButtonGroup } from "@mui/material";
import RadioButtonCheckedRounded from "@mui/icons-material/RadioButtonCheckedRounded";
import RadioButtonUncheckedRounded from "@mui/icons-material/RadioButtonUncheckedRounded";
import { Button, Tooltip } from "@mui/material";
let initialSelectedValue = '';
if (value === 1 || value === 0) {
initialSelectedValue = true;
} else {
initialSelectedValue = value;
}
This block determines the initial value for your component's state.
- It starts by declaring initialSelectedValue as an empty string.
- Then, it checks the value prop (or a variable named value available in this scope).
- If the value is 1 or 0, it sets initialSelectedValue to true. This suggests that 1 and 0 might be used as boolean indicators (like "on" or "off").
Otherwise, it sets initialSelectedValue to whatever value it is. This handles cases where value could be a string, a number other than 0 or 1, or any other data type.






Join the conversation! Your thoughts help the community grow.