Open In App

React MUI Chip Display

React MUI is a UI library that provides fully-loaded components, bringing our own design system to our production-ready components. MUI is a user interface library that provides predefined and customizable React components for faster and easy web development, these Material-UI components are based on top of Material Design by Google.

In this article, we’ll be discussing React MUI Chip Display. A chip is a compact element that represents an input, attribute, or action. They also allow the users to input information, make selections, filter the content, or trigger any actions.



React MUI Chip Props:

 



Syntax:

<Chip label="Submit" />

Creating React Project:

Step 1: To create a react app, you need to install react modules through the npm command.

npm create-react-app project name

Step 2: After creating your react project, move into the folder to perform different operations.

cd project name

Step 3: After creating the ReactJS application, Install the required module using the following command:

npm install @mui/material @emotion/react @emotion/styled

Project Structure:

 

Step to Run Application:

npm start

Example 1: Below example demonstrates the React MUI clickable chip of different colors and sizes.




import React from "react";
import Chip from "@mui/material/Chip";
import Stack from "@mui/material/Stack";
  
function App() {
  
    const handleClick = () => {
        alert('Clicked a chip');
    };
  
    return (
        <div>
            <div style={{ textAlign: "center", color: "green" }}>
                <h1>GeeksforGeeks</h1>
                <h2>React MUI Chip Display</h2>
            </div>
            <div>
                <Stack direction="row" spacing={1} style=
                    {{ justifyContent: 'center' }}>
                    <Chip label="Small" size="small" 
                        color="primary" onClick={handleClick} />
                    <Chip label="Small" size="small" 
                        color="success" onClick={handleClick} />
                    <Chip label="Medium" size="medium" 
                        variant="outlined" color="primary" 
                        onClick={handleClick} />
                    <Chip label="Medium" size="medium" 
                        variant="outlined" color="success" 
                        onClick={handleClick} />
                </Stack>
            </div>
        </div>
    );
}
  
export default App;

Output:

 

Example 2: Below example demonstrates the React MUI avatar and icon-based chip. We can add ornaments at the beginning of the chip component. We use the ‘avatar’ prop to add an avatar and use the ‘icon’ prop to add an icon.




import React from "react";
import Chip from "@mui/material/Chip";
import Stack from "@mui/material/Stack";
import { Avatar } from "@mui/material";
import FaceIcon from '@mui/icons-material/Face';
  
function App() {
    const handleClick = () => {
        console.log("Clicked");
    };
  
    return (
        <div>
            <div style={{ textAlign: "center", color: "green" }}>
                <h1>GeeksforGeeks</h1>
                <h2>React MUI Chip Display</h2>
            </div>
            <div>
                <Stack direction="row" spacing={1} 
                    style={{ justifyContent: "center" }}>
                    <Chip
                        label="GeeksforGeeks"
                        variant="contained"
                        onClick={handleClick}
                        icon={<FaceIcon />}
                    />
                    <Chip
                        label="GeeksforGeeks"
                        variant="outlined"
                        color="success"
                        onClick={handleClick}
                        avatar={<Avatar alt="Gfg" src=
                    />
                </Stack>
            </div>
        </div>
    );
}
  
export default App;

Output:

 

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


Article Tags :