Open In App

React MUI ListItemButton 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 ListItemButton API. The Lists are continuous, vertical indexes of Text and Images. ListItemButton is a list item that makes it functional or clickable. It also adds the material splash effect on top of the button. The API provides a lot of functionality and we will learn to implement them.



Import ListItemButton API

import ListItemButton from '@mui/material/ListItemButton';
// or
import { ListItemButton } 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 ListItemButton in List as follows

<ListItem>
    <ListItemButton>
        ...
    </ListItemButton>
</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 ListItemButton inside the List.




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 ListItemIcon from "@mui/material/ListItemIcon";
import ListItemText from "@mui/material/ListItemText";
import Divider from "@mui/material/Divider";
import InboxIcon from "@mui/icons-material/Inbox";
import DraftsIcon from "@mui/icons-material/Drafts";
import { Css, Html } 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 ListItemButton API</strong>
            </div>
            <br />
            <center>
                <Box sx={{ width: "100%", maxWidth: 360, bgcolor: 
                    "background.paper" }}>
                    <nav aria-label="main mailbox folders">
                        <List>
                            <ListItem>
                                <ListItemButton>
                                    <ListItemIcon>
                                        <Html />
                                    </ListItemIcon>
                                    <ListItemText 
                                    primary="HTML" />
                                </ListItemButton>
                            </ListItem>
                            <ListItem>
                                <ListItemButton>
                                    <ListItemIcon>
                                        <Css />
                                    </ListItemIcon>
                                    <ListItemText 
                                    primary="CSS" />
                                </ListItemButton>
                            </ListItem>
                        </List>
                    </nav>
                    <nav aria-label="secondary mailbox folders">
                        <List>
                            <ListItem disablePadding>
                                <ListItemButton>
                                    <ListItemText 
                                    primary="Javascript" />
                                </ListItemButton>
                            </ListItem>
                            <ListItem disablePadding>
                                <ListItemButton component="a"
                                    href="#simple-list">
                                    <ListItemText 
                                    primary="PHP" />
                                </ListItemButton>
                            </ListItem>
                        </List>
                    </nav>
                </Box>
            </center>
        </div>
    );
}
  
export default App;

Output:

 

Example 2: In the following example, we have a divider for all the ListItemButtons.




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 ListItemIcon from "@mui/material/ListItemIcon";
import ListItemText from "@mui/material/ListItemText";
import Divider from "@mui/material/Divider";
import InboxIcon from "@mui/icons-material/Inbox";
import DraftsIcon from "@mui/icons-material/Drafts";
import { Css, Html } 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 ListItemButton API</strong>
            </div>
            <br />
            <center>
                <Box sx={{ width: "100%", maxWidth: 360, 
                    bgcolor: "background.paper" }}>
                    <nav aria-label="main mailbox folders">
                        <List>
                            <ListItem>
                                <ListItemButton divider>
                                    <ListItemIcon>
                                        <Html />
                                    </ListItemIcon>
                                    <ListItemText primary="HTML" />
                                </ListItemButton>
                            </ListItem>
                            <ListItem>
                                <ListItemButton divider>
                                    <ListItemIcon>
                                        <Css />
                                    </ListItemIcon>
                                    <ListItemText primary="CSS" />
                                </ListItemButton>
                            </ListItem>
                        </List>
                    </nav>
                    <nav aria-label="secondary mailbox folders">
                        <List>
                            <ListItem disablePadding>
                                <ListItemButton divider>
                                    <ListItemText
                                    primary="Javascript" />
                                </ListItemButton>
                            </ListItem>
                            <ListItem disablePadding>
                                <ListItemButton 
                                    divider component="a"
                                    href="#simple-list">
                                    <ListItemText primary="PHP" />
                                </ListItemButton>
                            </ListItem>
                        </List>
                    </nav>
                </Box>
            </center>
        </div>
    );
}
  
export default App;

Output:

 

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


Article Tags :