How to make mui list with collapse in react js?

Hi Friends 👋,

Welcome To aGuideHub!

To make mui list with collapse in react js, you can use <Collapse in={open} timeout="auto" unmountOnExit> in List. It will make mui list with collapse in React JS.

Today, I am going to show you, How to make mui list with collapse in react js

Installation

Install the following packages mui list in react js.

npm

npm install @mui/material @emotion/react @emotion/styled

yarn

yarn add @mui/material @emotion/react @emotion/styled

Table of contents

  • Install MUI and create a new React app.
  • Import Material-UI list.
  • Use the list Component.

Step 1: Install MUI and create a new React app.

First you have to install the React project. You should use create-react-app command to create a new React project.

npx create-react-app my-app
cd my-app
npm start

Step 2: Import Material-UI list.

After installing MUI, you have to import your React component. To do this, add the following line to the top of your component file.

import React from 'react';
import { List, ListItem, ListItemText, Collapse } from '@mui/material';

Step 3: Use the list Component.

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.

<List>
  <ListItem button onClick={handleClick}>
    <ListItemText primary="Clickable item" />
  </ListItem>
  <Collapse in={open} timeout="auto" unmountOnExit>
    <List component="div" disablePadding>
      <ListItem button>
        <ListItemText primary="Item 1" />
      </ListItem>
      <ListItem button>
        <ListItemText primary="Item 2" />
          </ListItem>
        </List>
  </Collapse>
</List>

MUI material make mui list with collapse example.

The below code is an example, you need to import list Component. Then, you can use <Collapse in={open} timeout="auto" unmountOnExit> in List. Then it will make mui list with collapse in react js.

App.js

import React from 'react';
import { List, ListItem, ListItemText, Collapse } from '@mui/material';

function ListWithCollapse() {
  const [open, setOpen] = React.useState(false);

  const handleClick = () => {
    setOpen(!open);
  };

  return (
    <List>
      <ListItem button onClick={handleClick}>
        <ListItemText primary="Clickable item" />
      </ListItem>
      <Collapse in={open} timeout="auto" unmountOnExit>
        <List component="div" disablePadding>
          <ListItem button>
            <ListItemText primary="Item 1" />
          </ListItem>
          <ListItem button>
            <ListItemText primary="Item 2" />
          </ListItem>
        </List>
      </Collapse>
    </List>
  );
}

export default ListWithCollapse; 

In the above code example, I have used the @mui/material component and make mui list with collapse in react js.

React, list, with, collapse

All the best 👍

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts