Material UI List using React.js
Last Updated :
08 Jul, 2022
Lists are a continuous group of text or images. They are composed of items containing primary and supplemental actions, which are represented by icons and text. Material UI for React has this component available for us, and it is very easy to integrate. We can use the List Component in ReactJS using the following approach.
In this article, we will implement Material UI's List Component. We will create a list that will have some Geeks For Geeks Courses in it. We add more functionalities to that list like opening and closing a particular sublist.
Approach: First we will create a basic react app using some installations. We will make our new List Component reusing inbuilt list components with some styling using Material-UI. We are going to create a list that will have some Geeks For Geeks Courses in it. That list will be able to open and close by the List Header. Also, we will add a checkbox to show subitems of the list. On clicking the checkbox, the Sublist of GFG course details will also be shown accordingly. We will be using List, Collapse, and Checkbox components from Material UI. We will learn more about this while implementing it, now Let's start creating our List.
Creating React Application And Installing Module :
Step 1: Create a React application using the following command :
npx create-react-app foldername
Step 2: After creating your project folder i.e. folder name, move to it using the following command:
cd foldername
Step 3: After creating the React.js application, install the material-UI modules using the following command.
npm install @material-ui/core
npm install @mui/icons-material
npm install @mui/material
Project Structure: Now create a new file Header.js in the folder named "src". Our header component will reside in this file. Now the new project structure will look like this :
initial project structure
Changing the project structure: In your project directory, create a file named ListComponent.js in the src folder. Now your new project structure will look like this :
modified project structure
Step 4: Create the component ListComponent.js which we will use to display the List. We can expand the list by adding list items to it. To add icon after the list item's name we used the ListItemIcon component and to give a name to the list item we used ListItemText. For opening and closing the child list we used the Collapse component. IconButton is just icons but with the additional effects of buttons. ListItemText has two fields related to display text i.e primary and secondary. Primary displays the primary text i.e. in our example GFG course name. The secondary is the secondary content to display like additional information about the list item, here GFG course list details.
ListComponent.js
import React from "react";
import { makeStyles } from "@material-ui/core/styles";
// Importing material UI components
import List from "@material-ui/core/List";
import ListSubheader from "@material-ui/core/ListSubheader";
import ListItem from "@material-ui/core/ListItem";
import ListItemIcon from "@material-ui/core/ListItemIcon";
import ListItemSecondaryAction from "@material-ui/core/ListItemSecondaryAction";
import ListItemAvatar from "@material-ui/core/ListItemAvatar";
import ListItemText from "@material-ui/core/ListItemText";
import Grid from "@material-ui/core/Grid";
import IconButton from "@material-ui/core/IconButton";
import Avatar from "@material-ui/core/Avatar";
import Collapse from "@material-ui/core/Collapse";
import Checkbox from "@material-ui/core/Checkbox";
// Importing material UI icons
import InboxIcon from "@material-ui/icons/MoveToInbox";
import ExpandLess from "@material-ui/icons/ExpandLess";
import ExpandMore from "@material-ui/icons/ExpandMore";
import FolderIcon from "@material-ui/icons/Folder";
import DeleteIcon from "@material-ui/icons/Delete";
const useStyles = makeStyles((theme) => ({
root: {
width: "100%",
maxWidth: 360,
backgroundColor: theme.palette.background.paper,
},
nested: {
paddingLeft: theme.spacing(4),
},
demo: {
backgroundColor: theme.palette.background.paper,
},
}));
export default function ListComponent() {
const classes = useStyles();
const [open, setOpen] = React.useState(true);
const [secondary, setSecondary] = React.useState(false);
const handleClick = () => {
setOpen(!open);
};
return (
<Grid item xs={12} md={6}>
<div className={classes.demo}>
{/* If checkbox is ticked then secondary text will be shown otherwise not */}
<Checkbox
checked={secondary}
onChange={(event) => setSecondary(event.target.checked)}
/>
<List
component="nav"
aria-labelledby="nested-list-subheader"
subheader={
<ListSubheader component="div" id="nested-list-subheader">
mark the checkbox above to see sublist
</ListSubheader>
}
className={classes.root}
>
<ListItem button onClick={handleClick}>
<ListItemIcon>
<InboxIcon />
</ListItemIcon>
<ListItemText primary="Open Geeks for Geeks Courses" />
{/*code to open and closed list*/}
{open ? <ExpandLess /> : <ExpandMore />}
</ListItem>
<Collapse in={open} timeout="auto" unmountOnExit>
{/*List item are wrapped inside List */}
<List component="div" disablePadding>
<ListItem> {/* Single list item */}
<ListItemAvatar>
<Avatar>
<FolderIcon />
</Avatar>
</ListItemAvatar>
<ListItemText
primary="GFG Self-Paced Course"
secondary={
secondary ? "Structured premium video lectures" : null
}
/>
<ListItemSecondaryAction>
{/*Inside the IconButton, we can render various icons*/}
<IconButton edge="end" aria-label="delete">
<DeleteIcon />
</IconButton>
</ListItemSecondaryAction>
</ListItem>
<ListItem>
<ListItemAvatar>
<Avatar>
<FolderIcon />
</Avatar>
</ListItemAvatar>
<ListItemText
primary="Placement Preparation Course"
// if secondary variable state is true then show
// text otherwise null i.e. nothing will be shown
secondary={
secondary
? "An interview-centric course designed to prepare
you for the role of SDE"
: null
}
/>
<ListItemSecondaryAction>
<IconButton edge="end" aria-label="delete">
<DeleteIcon />
</IconButton>
</ListItemSecondaryAction>
</ListItem>
<ListItem>
<ListItemAvatar>
<Avatar>
<FolderIcon />
</Avatar>
</ListItemAvatar>
<ListItemText
primary="Live Course"
secondary={
secondary
? "Real Time Live Classes accessible from home"
: null
}
/>
<ListItemSecondaryAction>
<IconButton edge="end" aria-label="delete">
<DeleteIcon />
</IconButton>
</ListItemSecondaryAction>
</ListItem>
</List>
</Collapse>
</List>
</div>
</Grid>
);
}
Step 5: After creating the ListComponent component, we will import it in App.js and make changes in App.js as follows.
App.js
import React from "react";
import ListComponent from "./ListComponent";
function App() {
return (
<div>
{/* Use the newly created ListComponent component
in this main App component */}
<ListComponent />
</div>
);
}
export default App;
Step to run the application: Run the application using the following command from the root directory of the project.
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output.
list display output
Reference : https://mui.com/components/lists/
Similar Reads
React Material UI
MUI or Material UI is an open-source React Components library that is based on Google's Material Design and provides the predefined UI components for React.Material UI is a UI library that provides predefined react components implementing Google's Material Design. Material UI is a design language bu
5 min read
Onsen UI CSS Component Material List
Onsen UI CSS is used to create beautiful HTML components. It is one of the most efficient ways to create HTML5 hybrid components that are compatible with both mobile and desktop. Lists are used to store data or information on web pages in ordered or unordered form. Onsen UI CSS Component Material Li
2 min read
Onsen UI CSS Component Material List Title
Onsen UI CSS is used to create beautiful HTML components. It is one of the most efficient ways to create HTML5 hybrid components that are compatible with both mobile and desktop. Onsen UI CSS Component Material List Title is used to create the material list title at the top of the list items. To cre
2 min read
Recipe Finder using ReactJS
In this project article, we will be creating a recipe finder application using the React library. We have given the application the name "GeeksforGeeks Recipe Finder". This application is completely developed in ReactJS and mainly focuses on the functional components and also manages the various sta
5 min read
How to use Select Component in Material UI ?
Select components are used for collecting user-provided information from a list of options. Material UI for React has this component available for us and it is very easy to integrate. We can use the Select Component in ReactJS using the following approach:Creating React Application And Installing Mo
2 min read
How to add a Menu in react-native using Material Design ?
In this article, we will see how to create a menu in react-native. To make a menu, we will use the material design library, i.e., react-native-paper. The menu contains a list of options that appear temporarily. In our project, we will create a button, and the menu will appear on the click of that bu
9 min read
React MUI List 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 List API. Lists are continuous ranges of similar or identical items us
4 min read
Create ToDo App using ReactJS
In this article, we will create a to-do app to understand the basics of ReactJS. We will be working with class based components in this application and use the React-Bootstrap module to style the components. This to-do list can add new tasks we can also delete the tasks by clicking on them. The logi
3 min read
React MUI Lists Display
React Lists are a continuous group of text or images. They are composed of items containing primary and supplemental actions, which are represented by icons and text. Material UI is a library of React UI components that implements Google's Material Design. It includes a comprehensive collection of
4 min read
Create a To-Do List App using React Redux
A To-Do list allows users to manage their tasks by adding, removing, and toggling the completion status for each added item. It emphasizes a clean architecture, with Redux state management and React interface rendering. Todo AppPrerequisites Node.jsReactReact-ReduxApproachCreate a new React project
3 min read