React MUI ListItemAvatar 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 ListItemAvatar API. The Lists are continuous, vertical indexes of Text and Images. ListItemAvatar is a list item that allows placing an avatar in the avatar. The API provides a lot of functionality and we will learn to implement them.
import ListItemAvatar from '@mui/material/ListItemAvatar';
// or
import { ListItemAvatar } 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.
- classes(object): This overrides the existing styles or adds new styles to the component.
- sx( Array<func / object / bool>/ func / object): The system prop allows defining system overrides as well as additional CSS styles.Â
- children(node): The content of the ListItem is an avatar.
CSS Rules:
- root(.MuiListItemAvatar-root): The style applied to the root element.
- alignItemsFlexStart(.MuiListItemAvatar-alignItemsFlexStart): It is the style applied to the root element when the parent ListItem alignItems is set to flex-start.
Syntax: Create ListItemAvatar in List as follows
<ListItemAvatar> <Avatar alt="logo" src="/src" /> </ListItemAvatar>
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 ListItemAvatar inside the List.
App.js
import "./App.css"; import * as React from "react"; 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 ListItemAvatar from "@mui/material/ListItemAvatar"; import Checkbox from "@mui/material/Checkbox"; import Avatar from "@mui/material/Avatar";   function App() {     const [checked, setChecked] = React.useState([1]);     const handleToggle = (value) => () => {      const currentIndex = checked.indexOf(value);      const newChecked = [...checked];       if (currentIndex === -1) {         newChecked.push(value);       } else {        newChecked.splice(currentIndex, 1);       }       setChecked(newChecked);     };     return (      <div className="App">       <div        className="head"       style={{         width: "fit-content",         margin: "auto",        }}       >        <h1 style={{         color: "green",         }}        >         zambiatek        </h1>        <strong>React MUI ListItemAvatar API</strong>       </div>       <br />       <center>       <List dense        sx={{ width: "100%", maxWidth: 360,         bgcolor: "background.paper" }}       >        {[0, 1, 2, 3].map((value) => {         const labelId = `checkbox-list-secondary-label-${value}`;          return (           <ListItem            key={value}            secondaryAction={             <Checkbox              edge="end"             onChange={handleToggle(value)}              checked={checked.indexOf(value) !== -1}              inputProps={{ "aria-labelledby": labelId }}             />             }             disablePadding            >             <ListItemButton>              <ListItemAvatar>               <Avatar                alt="Logo" src=                shape="" />              </ListItemAvatar>              <ListItemText               id={labelId} primary={`List item ${value + 1}`}              />             </ListItemButton>            </ListItem>           );          })}         </List>        </center>      </div>    ); }   export default App; |
Output:
Â
Example 2: In the following example, we have customized the avatar shapes.
App.js
import "./App.css"; import * as React from "react"; 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 ListItemAvatar from "@mui/material/ListItemAvatar"; import Checkbox from "@mui/material/Checkbox"; import Avatar from "@mui/material/Avatar";   function App() {   const [checked, setChecked] = React.useState([1]);     const handleToggle = (value) => () => {     const currentIndex = checked.indexOf(value);     const newChecked = [...checked];       if (currentIndex === -1) {       newChecked.push(value);     } else {       newChecked.splice(currentIndex, 1);     }       setChecked(newChecked);   };     return (     <div className="App">       <div         className="head"        style={{           width: "fit-content",           margin: "auto",         }}       >         <h1           style={{             color: "green",           }}         >           zambiatek         </h1>         <strong>React MUI ListItemAvatar API</strong>       </div>       <br />       <center>         <List           dense           sx={{             width: "100%",             maxWidth: 360,             bgcolor: "background.paper"           }}         >           {[0, 1, 2, 3].map((value) => {             const labelId =               `checkbox-list-secondary-label-${value}`;             return (               <ListItem                 key={value}                 secondaryAction={                   <Checkbox                     edge="end"                    onChange={handleToggle(value)}                     checked={checked.indexOf(value) !== -1}                     inputProps={{ "aria-labelledby": labelId }}                   />                 }                 disablePadding               >                 <ListItemButton>                   <ListItemAvatar>                     <Avatar                       alt="Logo"                      src=                       variant="square"                    />                   </ListItemAvatar>                   <ListItemText                     id={labelId}                     primary={`List item ${value + 1}`}                   />                 </ListItemButton>               </ListItem>             );           })}         </List>       </center>     </div>   ); }   export default App; |
Output:
Â
Reference: https://mui.com/material-ui/api/list-item-avatar/



