How to use Collapse Component in ReactJS ?
Last Updated :
26 Jul, 2024
ReactJS, a popular JavaScript library for building user interfaces, provides developers with a rich set of components to create interactive and dynamic web applications. One such component is the Collapse component, which allows you to hide and reveal content based on user interaction. In this article, we will explore how to effectively use the Collapse component in ReactJS to create collapsible sections within your applications.
Prerequisites
Steps to create 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. foldername, move to it using the following command.
cd foldername
Step 3: After creating the ReactJS application, Install the material-ui modules using the following command.
npm install @material-ui/core
Project Structure:
Project StructureExample: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.
JavaScript
import React from 'react';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Paper from '@material-ui/core/Paper';
import Switch from '@material-ui/core/Switch';
import Collapse from '@material-ui/core/Collapse';
export default function App() {
const [isChecked, setIsChecked] = React.useState(false);
return (
<div style={{ display: 'block', padding: 30 }}>
<h4>How to use Collapse Component in ReactJS?</h4>
<FormControlLabel
control={<Switch checked={isChecked} onChange={() => {
setIsChecked((prev) => !prev);
}} />}
label="Toggle me to see Collapse Effect"
/>
<div style={{ display: 'flex' }}>
<Collapse in={isChecked}>
<Paper
elevation={5}
style={{ margin: 5 }} >
<svg style={{ width: 100, height: 100 }}>
<polygon points="0,80 45,00, 80,70"
style={{
fill: 'orange',
stroke: 'dimgrey',
strokeWidth: 1,
}} />
</svg>
</Paper>
</Collapse>
</div>
</div>
);
}
Step to Run 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.
Reference: https://material-ui.com/components/transitions/#collapse
Similar Reads
How to use Zoom Component in ReactJS? Zoom Component adds a Zoom animation to a child element or component. Material UI for React has this component available for us, and it is very easy to integrate. We can use the Zoom Component in ReactJS using the following approach.Prerequisites:NodeJS or NPMReact JSMaterial UIApproach:The React co
2 min read
How to use Grow Component in ReactJS? Grow Component in ReactJS adds a Grow animation to a child element or component. Material UI for React has this component available for us, and it is very easy to integrate. We can use the Grow Component in ReactJS using the following approach.PrerequisitesReact JSMaterial UIApproachTo implement the
2 min read
How To Use Modal Component In ReactJS? Modals are a popular UI component used to display content in a pop-up window like alerts, forms, or additional information. In ReactJS, there are multiple ways to implement a modal. In this article, we will discuss two common approaches: using reusable functional components and using the Material-UI
4 min read
How to use Popper Component in ReactJS ? A Popper is used to show the part of the content on top of another. It's an alternative feature for react-popper. Material UI for React has this component available for us, and it is simple and very easy to integrate. For perfect positioning, it uses 3rd party library which is Popper.js.Prerequisite
3 min read
How to use Portal Component in ReactJS ? The portal component renders its children into a new subtree outside the current DOM hierarchy. Material UI for React has this component available for us, and it is very easy to integrate. We can use the Portal Component in ReactJS using the following approach.Prerequisites:NodeJS or NPMReact JSMate
2 min read
ReactJS UI Ant Design Collapse Component Ant Design, often referred to as AntD, is a popular React UI library that provides a set of components for building interactive and responsive web applications. Among these components, the Collapse component is particularly useful for creating collapsible sections, which can help organize content in
3 min read