Open In App

React MUI ListItemIcon API

Material-UI is a UI library providing predefined robust and customizable components for React for easier web development. The MUI design is based on top of Material Design by Google.

In this article, we will discuss the React MUI ListItemIcon API. The Lists are continuous, vertical indexes of Text and Images. ListItem is a single item that contains the individual content. The ListItemIcon is used to display an icon in the ListItem. The API provides a lot of functionality and we will learn to implement them.



Import ListItemIcon API:

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

Props List: Here is the list of props used with this component. We can access them and modify them according to our needs.



CSS Rules:

Syntax: Create ListItemIcon in List as follows.

<ListItem>
    <ListItemIcon>
        <Javascript />
    </ListItemIcon>
</ListItem>

Installing and Creating React app, and adding the MUI dependencies.

Step 1: Create a react project using the following command.

npx create-react-app gfg_tutorial

Step 2: Get into the project directory

cd gfg_tutorial

Step 3: Install the MUI dependencies as follows:

npm install @mui/material @emotion/react 
npm install @emotion/styled @mui/lab @mui/icons-material

Project Structure: 

 

Step 4: Run the project as follows:

npm start

Example 1: In the following example, we have ListItemIcon inside the ListItem.




import "./App.css";
import * as React from "react";
import Box from "@mui/material/Box";
import List from "@mui/material/List";
import ListItem from "@mui/material/ListItem";
import ListItemButton from "@mui/material/ListItemButton";
import ListItemText from "@mui/material/ListItemText";
import { ListItemIcon } from "@mui/material";
import { Css, Html, Javascript, Php } from "@mui/icons-material";
  
function App() {
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI ListItemIcon API</strong>
            </div>
            <br />
            <center>
                <Box sx={{ width: "100%", maxWidth: 360,
                           bgcolor: "background.paper" }}>
                    <nav>
                        <List>
                            <ListItem divider>
                                <ListItemButton>
                                    <ListItemIcon>
                                        <Html />
                                    </ListItemIcon>
                                    <ListItemText 
                                        primary="HTML" />
                                </ListItemButton>
                            </ListItem>
                            <ListItem divider>
                                <ListItemButton>
                                    <ListItemIcon>
                                        <Css />
                                    </ListItemIcon>
                                    <ListItemText 
                                        primary="CSS" />
                                </ListItemButton>
                            </ListItem>
  
                            <ListItem divider>
                                <ListItemButton>
                                    <ListItemIcon>
                                        <Javascript />
                                    </ListItemIcon>
                                    <ListItemText 
                                        primary="Javascript" />
                                </ListItemButton>
                            </ListItem>
                            <ListItem divider>
                                <ListItemButton>
                                    <ListItemIcon>
                                        <Php />
                                    </ListItemIcon>
                                    <ListItemText 
                                        primary="PHP" />
                                </ListItemButton>
                            </ListItem>
                        </List>
                    </nav>
                </Box>
            </center>
        </div>
    );
}
  
export default App;

Output:

 

Example 2: In the following example, we have different colors for different ListItemIcons.




import "./App.css";
import * as React from "react";
import Box from "@mui/material/Box";
import List from "@mui/material/List";
import ListItem from "@mui/material/ListItem";
import ListItemButton from "@mui/material/ListItemButton";
import ListItemText from "@mui/material/ListItemText";
import { ListItemIcon } from "@mui/material";
import { Css, Html, Javascript, Php } from "@mui/icons-material";
  
function App() {
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI ListItemIcon API</strong>
            </div>
            <br />
            <center>
                <Box sx={{ width: "100%", maxWidth: 360,
                           bgcolor: "background.paper" }}>
                    <nav>
                        <List>
                            <ListItem divider>
                                <ListItemButton>
                                    <ListItemIcon 
                                        sx={{ color: "red" }}>
                                        <Html />
                                    </ListItemIcon>
                                    <ListItemText 
                                        primary="HTML" />
                                </ListItemButton>
                            </ListItem>
                            <ListItem divider>
                                <ListItemButton>
                                    <ListItemIcon 
                                        sx={{ color: "orange" }}>
                                        <Css />
                                    </ListItemIcon>
                                    <ListItemText 
                                        primary="CSS" />
                                </ListItemButton>
                            </ListItem>
  
                            <ListItem divider>
                                <ListItemButton>
                                    <ListItemIcon 
                                        sx={{ color: "purple" }}>
                                        <Javascript />
                                    </ListItemIcon>
                                    <ListItemText 
                                        primary="Javascript" />
                                </ListItemButton>
                            </ListItem>
                            <ListItem divider>
                                <ListItemButton>
                                    <ListItemIcon 
                                        sx={{ color: "green" }}>
                                        <Php />
                                    </ListItemIcon>
                                    <ListItemText 
                                        primary="PHP" />
                                </ListItemButton>
                            </ListItem>
                        </List>
                    </nav>
                </Box>
            </center>
        </div>
    );
}
  
export default App;

Output:

 

Reference: https://mui.com/material-ui/api/list-item-icon/


Article Tags :