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 ListItemSecondaryAction API. The Lists are continuous, vertical indexes of Text and Images. ListItem is a single item that contains the individual content. The ListItemSecondaryAction will enable to place a secondary action on an item. The API provides a lot of functionality and we will learn to implement them.
Import ListItemSecondaryAction API
import ListItemSecondaryAction from '@mui/material/ListItemSecondaryAction'; // or import { ListItemSecondaryAction } 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.
- children(node): It is the content of the component.
- classes(object): This overrides the existing styles or adds new styles to the component.
- sx( Array<func / object / bool>/ func / object): This system prop allows defining system overrides as well as additional CSS styles.
CSS Rules:
- root(.MuiListItemSecondaryAction-root): It is the style applied to the root element.
- disableGutters(.MuiListItemSecondaryAction-disableGutters): It is the style applied to the root element when the parent ListItem has disableGutters set to true.
Syntax: Create ListItemSecondaryAction in List as follows.
<ListItem divider> <ListItemSecondaryAction onClick={() => { alert("You clicked on the ListItem"); }} > <Typography> HTML </Typography> </ListItemSecondaryAction> </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 the ListItemSecondaryAction item inside the List. It shows an alert on click.
App.js
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, ListItemSecondaryAction, Typography, } 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" , }} > neveropen </h1> <strong>React MUI ListItemSecondaryAction API</strong> </div> <br /> <center> <Box sx={{ width: "100%" , maxWidth: 360, bgcolor: "background.paper" }}> <nav> <List> <ListItem divider> <ListItemButton> <ListItemSecondaryAction onClick={() => { alert( "You clicked on the ListItem" ); }} > <Typography variant= "body1" color= "textprimary" > HTML </Typography> </ListItemSecondaryAction> </ListItemButton> </ListItem> <ListItem divider> <ListItemButton> <ListItemSecondaryAction onClick={() => { alert( "You clicked on the ListItem" ); }} > <Typography variant= "body1" color= "textprimary" > CSS </Typography> </ListItemSecondaryAction> </ListItemButton> </ListItem> <ListItem divider> <ListItemButton> <ListItemSecondaryAction onClick={() => { alert( "You clicked on the ListItem" ); }} > <Typography variant= "body1" color= "textprimary" > Javascript </Typography> </ListItemSecondaryAction> </ListItemButton> </ListItem> </List> </nav> </Box> </center> </div> ); } export default App; |
Output:
Example 2: In the following example, we count the number of clicks and display them.
App.js
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 { useState } from "react" ; import { ListItemIcon, ListItemSecondaryAction, Typography, } from "@mui/material" ; function App() { const [count, setCount] = useState(0); return ( <div className= "App" > <div className= "head" style={{ width: "fit-content" , margin: "auto" , }} > <h1 style={{ color: "green" , }} > neveropen </h1> <strong>React MUI ListItemSecondaryAction API</strong> </div> <br /> <center> <Box sx={{ width: "100%" , maxWidth: 360, bgcolor: "background.paper" }}> <nav> <List> <ListItem divider> <ListItemButton> <ListItemSecondaryAction onClick={() => { setCount(count + 1); }} > <Typography variant= "body1" color= "textprimary" > HTML </Typography> </ListItemSecondaryAction> </ListItemButton> </ListItem> <ListItem divider> <ListItemButton> <ListItemSecondaryAction onClick={() => { setCount(count + 1); }} > <Typography variant= "body1" color= "textprimary" > CSS </Typography> </ListItemSecondaryAction> </ListItemButton> </ListItem> <ListItem divider> <ListItemButton> <ListItemSecondaryAction onClick={() => { setCount(count + 1); }} > <Typography variant= "body1" color= "textprimary" > Javascript </Typography> </ListItemSecondaryAction> </ListItemButton> </ListItem> </List> <Typography variant= "title1" color= "textprimary" > Count: {count} </Typography> </nav> </Box> </center> </div> ); } export default App; |
Output:
Reference: https://mui.com/material-ui/api/list-item-secondary-action/#main-content