React Accordion - Getting Started

NOTE
Before you start the tutorial, ensure DevExtreme is installed in your application.

The Accordion UI component contains several panels displayed one under another.

This tutorial shows how to add an Accordion to the page and configure the component's core settings. As a result, you will create the following UI component:

Each section in this tutorial describes a single configuration step. You can also find the full source code in the GitHub repository.

View on GitHub

Create an Accordion

Add DevExtreme to your React application and use the following code to create an Accordion:

App.js
  • import React from 'react';
  •  
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import { Accordion } from 'devextreme-react/accordion';
  •  
  • function App() {
  • return (
  • <Accordion />
  • );
  • }
  •  
  • export default App;

Bind Accordion to Data

You can display Accordion data from dataSource or items array. Note that each data source object must contain the title field whose value specifies the panel's title. If you want to customize the title, refer to the following section of this tutorial: Customize Item Appearance.

App.js
data.js
  • // ...
  • import { employees } from './data';
  •  
  • function App() {
  • return (
  • <Accordion
  • dataSource={employees}
  • />
  • );
  • }
  •  
  • export default App;
  • export const employees = [{
  • ID: 1,
  • Prefix: 'Mr.',
  • FirstName: 'John',
  • LastName: 'Heart',
  • Position: 'CEO',
  • State: 'California',
  • BirthDate: '1964/03/16',
  • },
  • {
  • ID: 2,
  • Prefix: 'Mrs.',
  • FirstName: 'Olivia',
  • LastName: 'Peyton',
  • Position: 'Sales Assistant',
  • State: 'California',
  • BirthDate: '1981/06/03',
  • },
  • {
  • ID: 3,
  • Prefix: 'Mr.',
  • FirstName: 'Robert',
  • LastName: 'Reagan',
  • Position: 'CMO',
  • State: 'Arkansas',
  • BirthDate: '1974/09/07',
  • },
  • {
  • ID: 4,
  • Prefix: 'Ms.',
  • FirstName: 'Greta',
  • LastName: 'Sims',
  • Position: 'HR Manager',
  • State: 'Georgia',
  • BirthDate: '1977/11/22',
  • }];

Control the Accordion Behavior

If you do not specify additional properties, only one panel can be expanded at a time. To change this behavior, set the collapsible and multiple properties to true. You can also use the animationDuration property to change the duration of the panel expand and collapse animations.

App.js
  • // ...
  •  
  • function App() {
  • return (
  • <Accordion ...
  • collapsible={true}
  • multiple={true}
  • animationDuration="450"
  • />
  • );
  • }
  •  
  • export default App;

Customize Item Appearance

To customize panel appearance, use the itemTemplate for panel content and the itemTitleTemplate for the panel's title.

App.js
  • // ...
  • const customTitle = (data) => {
  • return data.FirstName + " " + data.LastName;
  • }
  • const customItem = (data) => {
  • return data.Position + " from " + data.State;
  • }
  •  
  • function App() {
  • return (
  • <Accordion ...
  • itemTitleRender={customTitle}
  • itemRender={customItem}
  • />
  • );
  • }
  •  
  • export default App;