Getting Started
28 Jun 202513 minutes to read
This section briefly explains you the steps required to create a simple React Accordion and demonstrate the basic usage of the Accordion control.
Dependencies
The following list of dependencies are required to use the React Accordion component in your application.
|-- @syncfusion/ej2-react-navigations
|-- @syncfusion/ej2-base
|-- @syncfusion/ej2-react-base
|-- @syncfusion/ej2-navigations
|-- @syncfusion/ej2-buttons
|-- @syncfusion/ej2-popups
Setup for Local Development
To easily set up a React application, use create-vite-app
, which provides a faster development environment, smaller bundle sizes, and optimized builds compared to traditional tools like create-react-app
. For detailed steps, refer to the Vite installation instructions. Vite sets up your environment using JavaScript and optimizes your application for production.
Note: To create a React application using
create-react-app
, refer to this documentation for more details.
To create a new React application, run the following command.
npm create vite@latest my-app
To set-up a React application in TypeScript environment, run the following command.
npm create vite@latest my-app -- --template react-ts
cd my-app
npm run dev
To set-up a React application in JavaScript environment, run the following command.
npm create vite@latest my-app -- --template react
cd my-app
npm run dev
Adding Syncfusion® packages
All the available Essential® JS 2 packages are published in npmjs.com
public registry.
To install Accordion component, use the following command
npm install @syncfusion/ej2-react-navigations --save
Adding CSS reference
Add components style as given below in src/App.css
.
@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../node_modules/@syncfusion/ej2-react-navigations/styles/material.css';
To refer
App.css
in the application then import it in thesrc/App.tsx
file.
Initialize the Accordion using Items
The React Accordion can be rendered by defining an array of items
.
- Import the Accordion component to your
src/App.tsx
file using following code.
import { AccordionComponent, AccordionItemDirective, AccordionItemsDirective } from '@syncfusion/ej2-react-navigations';
import * as React from 'react';
const ReactApp = () => {
const aspContent = () => {
return (<div>
Microsoft ASP.NET is a set of technologies in the Microsoft .NET Framework for building Web applications and XML Web services.
</div>);
}
const mvcContent = () => {
return (<div>
The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller.
</div>);
}
const jsContent = () => {
return (<div>
JavaScript (JS) is an interpreted computer programming language.It was originally implemented as part of web browsers so that client-side scripts could interact with the user, control the browser, communicate asynchronously, and alter the document content that was displayed.
</div>);
}
return (
<AccordionComponent>
<AccordionItemsDirective>
<AccordionItemDirective header='ASP.NET' content={aspContent} />
<AccordionItemDirective header='ASP.NET MVC' content={mvcContent} />
<AccordionItemDirective header='JavaScript' content={jsContent} />
</AccordionItemsDirective>
</AccordionComponent>
);
}
export default ReactApp;
Now run the npm run dev
command in the console to start the development server. This command compiles your code and serves the application locally, opening it in the browser.
npm run dev
In the above sample code,
#element
is theid
of the HTML element in a page to which the Accordion is initialized.
Initialize the Accordion using HTML elements
The React Accordion component can be rendered based on the given HTML element using id
as target
property.
You need to follow the below structure of HTML elements to render the Accordion.
<div id='accordion_html_markup'> --> Root Accordion Element
<div> --> Accordion Item Container
<div> --> Accordion Header Container
<div> </div> --> Accordion Header
</div>
<div> --> Accordion Panel Container
<div> </div> --> Accordion Content
</div>
</div>
</div>
import { AccordionComponent } from '@syncfusion/ej2-react-navigations';
import * as React from "react";
import * as ReactDOM from "react-dom";
const ReactApp = () => {
return (<AccordionComponent>
<div>
<div>
<div> ASP.NET </div>
</div>
<div>
<div>
Microsoft ASP.NET is a set of technologies in the Microsoft .NET Framework for building Web applications and XML Web services.
</div>
</div>
</div>
<div>
<div>
<div> ASP.NET MVC </div>
</div>
<div>
<div>
The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller.
</div>
</div>
</div>
<div>
<div>
<div> JavaScript </div>
</div>
<div>
<div>
JavaScript (JS) is an interpreted computer programming language.It was originally implemented as part of web browsers so that client-side scripts could interact with the user, control the browser.
</div>
</div>
</div>
</AccordionComponent>);
}
const root = ReactDOM.createRoot(document.getElementById('element'));
root.render(<ReactApp />);
import { AccordionComponent } from '@syncfusion/ej2-react-navigations';
import * as React from "react";
import * as ReactDOM from "react-dom";
const ReactApp = () => {
return (
<AccordionComponent>
<div>
<div>
<div> ASP.NET </div>
</div>
<div>
<div>
Microsoft ASP.NET is a set of technologies in the Microsoft .NET Framework for building Web applications and XML Web services.
</div>
</div>
</div>
<div>
<div>
<div> ASP.NET MVC </div>
</div>
<div>
<div>
The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller.
</div>
</div>
</div>
<div>
<div>
<div> JavaScript </div>
</div>
<div>
<div>
JavaScript (JS) is an interpreted computer programming language.It was originally implemented as part of web browsers so that client-side scripts could interact with the user, control the browser.
</div>
</div>
</div>
</AccordionComponent>
);
}
const root = ReactDOM.createRoot(document.getElementById('element'));
root.render(<ReactApp />);
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 React Accordion Sample</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Essential JS 2 for React Components" />
<meta name="author" content="Syncfusion" />
<link href="https://cdn.syncfusion.com/ej2/30.1.37/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<style>
#loader {
color: #008cff;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}
</style>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='element'>
<div id='loader'>Loading....</div></div>
</body>
</html>
You can add the custom class into Accordion component using
cssClass
property which is used to customize the Accordion component.
See Also
NOTE
You can also explore our React Accordion example that shows you how to configure the Accordion in React.