Open In App

React MUI Chip API

Chips are elements that represent an action, an attribute, or an input. Material UI is one of the most popular React Frontend libraries. It provides tons of ready-to-use components, hence saving the time to create our styles and presenting a good design. These components are easily customizable and hence they are easy to pick up. The Chip component of MUI is one of the components available in MUI. We will see how to use them in our React project.

Chip props:



Creating React Application: Follow the below steps to create a React.js application:

Step 1: Create the React project using the command given below



npx create-react-app mui-chip

Step 2: Navigate into the React project created. Open your preferred code editor/IDE.

cd mui-chip

Step 3: Install the MUI dependencies as suggested by the MUI team. Enter the command given below in your terminal.

npm install @mui/material @emotion/react @emotion/styled
# or
yarn add @mui/material @emotion/react @emotion/styled

Step 4 (Optional): Install the MUI icons dependency to use Material icons in our project

npm install @mui/icons-material
# or
yarn add @mui/icons-material

Project Structure: After creating the React project and installing all the dependencies, the project structure would look similar to the figure given below.

 

 

Importing MUI’s Chip component: In your JSX file, import the Chip component by using the below import line.

import Chip from '@mui/material/Chip';
// or
import { Chip } from '@mui/material';

Example 1: Variants of chip




import Chip from '@mui/material/Chip';
  
export default function App() {
    return (
        <div>
            <Chip label="Filled" />
            <Chip label="Outlined" variant="outlined" />
        </div>
    );
}

Steps to run the application: Run the application by entering the given command in the terminal at the root directory of the project.

npm start
# or 
yarn start

Output:

Variants of chip

Example 2: onClick and onDelete prop




import Chip from "@mui/material/Chip";
import DoneIcon from "@mui/icons-material/Done";
import { useState } from "react";
  
export default function App() {
    const [message, setMessage] = useState("Default message");
  
    function handleDelete() {
        setMessage("deleted");
    }
  
    function handleClick() {
        setMessage("clicked");
    }
  
    return (
        <div>
            <Chip label="Clickable chip" 
                onClick={handleClick} />
            <Chip label="Deletable chip" 
                onDelete={handleDelete} />
            <Chip
                label="Custom delete icon"
                onDelete={handleDelete}
                deleteIcon={<DoneIcon />}
            />
            <p>Message - {message}</p>
        </div>
    );
}

Explanation: The handleClick function sets the message to “clicked” and the handleDelete function sets the message to “deleted”. handleClick is called when the chip is clicked. handleDelete is called when the chip’s delete icon is clicked.

Output:

 

Example 3: Colors of Chip




import Stack from '@mui/material/Stack';
import Chip from '@mui/material/Chip';
  
export default function App() {
    return (
        <Stack direction="row" gap={3}>
            <Chip label="Default" />
            <Chip label="Primary" color="primary" />
            <Chip label="Secondary" color="secondary" />
            <Chip label="Error" color="error" />
            <Chip label="Info" color="info" />
            <Chip label="Success" color="success" />
            <Chip label="Warning" color="warning" />
        </Stack>
    );
}

Output:

Chips with different values of color prop

Example 4: icon and avatar prop




import Chip from "@mui/material/Chip";
import DoneIcon from "@mui/icons-material/Done";
import { Avatar, Stack } from "@mui/material";
  
export default function App() {
  
    return (
        <Stack direction="row" gap={3}>
            <Chip avatar={<Avatar>G</Avatar>} 
                color="success" label="GeeksForGeeks" />
            <Chip icon={<DoneIcon />} 
                color="success" label="Geeks filled" />
            <Chip icon={<DoneIcon />} 
                color="success" label="Geeks outlined" 
                variant="outlined" />
        </Stack>
    );
}

Output:

avatar and icon props in MUI Chip

 

 

Reference: https://mui.com/material-ui/react-chip/


Article Tags :