How to make mui chip with checkbox in react js?
July 19, 2023Hi Friends 👋,
Welcome To aGuideHub!
To make mui chip with checkbox in react js, you can use <Chip>
component and then put this code deleteIcon={<Checkbox checked={checked} />}
. it will make mui chip with checkbox in React JS.
Today, I am going to show you, How to make mui chip with checkbox in react js.
Installation
Install the following packages to use mui chip 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 chip.
- Use the chip 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 chip.
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 { Chip, Checkbox } from "@mui/material";
Step 3: Use the chip Component
You can use the chip
component in your react js. For example, Chips allow users to enter information, make selections, filter content, or initiate actions. You can use <chip>
component make a chip in react js.
<Chip
label="Clickable Chip"
onClick={() => console.log("Chip clicked!")}
onDelete={handleCheckboxChange}
deleteIcon={<Checkbox checked={checked} />}
/>
MUI material make mui chip with checkbox example.
The below code is an example, you need to import chip
Component.
Checkbox component from @mui/material
, passing the checked state as a prop to show whether the checkbox is checked.
App.js
import React, { useState } from "react";
import { Chip, Checkbox } from "@mui/material";
const MyCheckbox = () => {
const [checked, setChecked] = useState(false);
const handleCheckboxChange = () => {
setChecked((prevChecked) => !prevChecked);
};
return (
<Chip
label="Clickable Chip"
onClick={() => console.log("Chip clicked!")}
onDelete={handleCheckboxChange}
deleteIcon={<Checkbox checked={checked} />}
/>
);
};
export default MyCheckbox;
In the above code example, I have used the @mui/material
component and made mui chip with checkbox in react js.
Check the output of the above code example.
Here, we are provided code sandbox links for the above program make mui chip with checkbox in react js. Then you can use whenever you want and do the changes as per your requirements.
All the best 👍