Getting Started
7 Nov 202313 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 set-up a React application, choose any of the following ways. The best and easiest way is to use the create-react-app. It sets up your development environment in JavaScript and improvise your application for production. Refer to the installation instructions of create-react-app
.
npx create-react-app my-app
cd my-app
npm start
or
yarn create react-app my-app
cd my-app
yarn start
To set-up a React application in TypeScript
environment, run the following command.
npx create-react-app my-app --template typescript
cd my-app
npm start
Besides using the npm package runner tool, also create an application from the npm init
. To begin with the npm init
, upgrade the npm
version to npm 6+
.
npm init react-app my-app
cd my-app
npm start
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.ts
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.ts
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;
- Run the application in the browser using the following command.
npm start
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/28.1.33/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.