How to show error on mui radio in react js?
July 28, 2023Hi Friends 👋,
Welcome To aGuideHub!
To show error on mui radio in react js, you can use error={error}
and helperText={error ? "Please select an option" : ""}
in MyRadio. it will show error on mui radio in React JS.
Today, I am going to show you, How to show error on mui radio in react js.
Installation
Install the following packages to use mui radio in react js.
npm
npm install @mui/material @emotion/react @emotion/styled
yarn
yarn add @mui/material @emotion/react @emotion/styled
Table of contents
- Install MUI and create a new React app.
- Import Material-UI radio.
- Use the radio Component
Step 1: Install MUI and create a new React app.
First you have to install the React project. You should use create-react-app
command to create a new React project.
npx create-react-app my-app
cd my-app
npm start
Step 2: Import Material-UI radio.
After installing MUI
, you have to import your React component. To do this, add the following line to the top of your component file.
import React, { useState } from "react";
import MyRadio from "./MyRadio";
Step 3: Use the Radio Component.
You can use the Radio
component in your react js. For example, Radio buttons let users make a mutually exclusive choice. Only one selection is allowed from the available set of options. You can use error={error}
and helperText={error ? "Please select an option" : ""}
component show error on mui radio in react js.
<MyRadio
checked={selectedValue === "option1"}
onChange={handleChange}
label="Option 1"
error={error}
helperText={error ? "Please select an option" : ""}
/>
MUI material show error on mui radio example.
The below code is an example, you need to import Radio
Component. Then, you can create a Radio button and show the error on radio, you can use error={error}
and helperText={error ? "Please select an option" : ""}
.
App.js
import React, { useState } from "react";
import MyRadio from "./MyRadio";
const ParentComponent = () => {
const [selectedValue, setSelectedValue] = useState("");
const [error, setError] = useState(false);
const handleChange = (event) => {
setSelectedValue(event.target.value);
setError(false); // Clear the error when a value is selected
};
const handleSubmit = () => {
if (!selectedValue) {
setError(true); // Show the error if no value is selected on form submission
} else {
// Handle form submission with the selected value
}
};
return (
<div>
<MyRadio
checked={selectedValue === "option1"}
onChange={handleChange}
label="Option 1"
error={error}
helperText={error ? "Please select an option" : ""}
/>
<MyRadio
checked={selectedValue === "option2"}
onChange={handleChange}
label="Option 2"
error={error}
helperText={error ? "Please select an option" : ""}
/>
<button onClick={handleSubmit}>Submit</button>
</div>
);
};
export default ParentComponent;
MyRadio.js
import React from "react";
import Radio from "@mui/material/Radio";
import FormControlLabel from "@mui/material/FormControlLabel";
import FormHelperText from "@mui/material/FormHelperText";
const MyRadio = ({ checked, onChange, label, error, helperText }) => {
return (
<>
<FormControlLabel
control={<Radio checked={checked} onChange={onChange} />}
label={label}
/>
{error && <FormHelperText error>{helperText}</FormHelperText>}
</>
);
};
export default MyRadio;
In the above code example, I have used the @mui/material
component and show error on mui radio in react js.
Check the output of the above code example.
Here, we are provided code sandbox links for the above program show error on mui radio in react js. Then you can use whenever you want and do the changes as per your requirements.
All the best 👍