Options for building React Native collapsible accordions

React Native developers typically use UI component libraries to build apps. Even though modern mobile devices come with higher screen resolutions, a generic smartphone screen is still relatively small compared to computer screens, so app developers often need to reduce app UI content. There are various strategies to achieve this, such as using a collapsible segment that only shows its content with a screen tap to reduce the visible content in a user-friendly manner.

In some apps, we can toggle collapsible panel lists (a.k.a., accordion elements) with screen touch events. An accordion element typically lists multiple panel headers with summarized titles. Users can expand and collapse a collapsible panel to see more content by clicking or tapping the related header.

Every software development requirement typically has multiple possible solutions. Similarly, in this tutorial, I’ll explain the available options for building collapsible accordions in React Native with practical examples. We can use the following approaches for implementing accordions in React Native:

  • Developing an accordion with primary inbuilt React Native components (i.e., View, TouchableOpacity), vector icons, and inbuilt Animation APIs
  • Using a pre-developed accordion component from a third-party React Native package
  • Importing a pre-developed accordion from a popular React Native UI kit

Let’s start with a practical session and develop accordions based on the above options!

Jump ahead:

Creating a simple accordion without animations

Most accordions play expanding/collapsing animations by default, but in this section, we’ll build one without animations. Create a new React Native project to get started:

npx react-native init AccordionFromScratch
cd AccordionFromScratch

Starting from v0.71, React Native generates the initial project template with TypeScript by default. The above command will scaffold a project with the App.tsx source file; we’ll also use TypeScript in this tutorial.

Run the app with npx react-native run-android or npx react-native run-ios to make sure the project creation was successful.

We’ll use two vector icons from the react-native-vector-icons package. You can install it according to the official installation guide.

Next, erase everything in your App.tsx and add the following imports and the type definition:

import React, { useState } from 'react';
import type { PropsWithChildren } from 'react';
import { SafeAreaView, ScrollView, StyleSheet, Text, TouchableOpacity, View, Button,
} from 'react-native';
import Icon from 'react-native-vector-icons/FontAwesome'; type AccordionItemPros = PropsWithChildren<{ title: string;
}>;

Accordions usually have several collapsible elements. We can start implementing the accordion component with a single collapsible component. Create AccordionItem with the following code:

function AccordionItem({ children, title }: AccordionItemPros): JSX.Element { const [ expanded, setExpanded ] = useState(false); function toggleItem() { setExpanded(!expanded); } const body = <View style={styles.accordBody}>{ children }</View>; return ( <View style={styles.accordContainer}> <TouchableOpacity style={styles.accordHeader} onPress={ toggleItem }> <Text style={styles.accordTitle}>{ title }</Text> <Icon name={ expanded ? 'chevron-up' : 'chevron-down' } size={20} color="#bbb" /> </TouchableOpacity> { expanded && body } </View> );
}

The above collapsible component toggles the expanded/collapsed state when the user presses the panel header by using the expanded state field. Here, we change the arrow vector icon and accordion body section based on the expanded state field. This component accepts the accordion item title via a prop and accordion body elements via the component children prop.

Now, we can use the AccordionItem component and create an accordion within the App component as follows:

function App(): JSX.Element { return ( <SafeAreaView style={styles.container}> <ScrollView contentInsetAdjustmentBehavior="automatic" style={styles.container}> <AccordionItem title="Native development"> <Text style={styles.textSmall}>React Native lets you create truly native apps and doesn't compromise your users' experiences. It provides a core set of platform agnostic native components </Text> </AccordionItem> <AccordionItem title="Fast refresh"> <Text style={styles.textSmall}>See your changes as soon as you save. With the power of JavaScript, React Native lets you iterate at lightning speed.</Text> </AccordionItem> <AccordionItem title="Cross-platform"> <Text style={styles.textSmall}>React components wrap existing native code and interact with native APIs via React’s declarative UI paradigm and JavaScript. This enables native app development for whole new teams of developers</Text> <View style={styles.seperator}></View> <Button title="See more..."/> </AccordionItem> </ScrollView> </SafeAreaView> );
}

Look at the above code snippet and check how we added body content to our accordion body — we can use any component inside the accordion body. We’ve added a Button component instance in the third collapsible panel body for demonstration purposes.

Finally, we can add styling definitions and export the App component:

const styles = StyleSheet.create({ container: { flex: 1 }, accordContainer: { paddingBottom: 4 }, accordHeader: { padding: 12, backgroundColor: '#666', color: '#eee', flex: 1, flexDirection: 'row', justifyContent:'space-between' }, accordTitle: { fontSize: 20, }, accordBody: { padding: 12 }, textSmall: { fontSize: 16 }, seperator: { height: 12 }
}); export default App;

Here is the complete source code for building an accordion without expanding/collapsing animations:

import React, { useState } from 'react';
import type { PropsWithChildren } from 'react';
import { SafeAreaView, ScrollView, StyleSheet, Text, TouchableOpacity, View, Button,
} from 'react-native';
import Icon from 'react-native-vector-icons/FontAwesome'; type AccordionItemPros = PropsWithChildren<{ title: string;
}>; function AccordionItem({ children, title }: AccordionItemPros): JSX.Element { const [ expanded, setExpanded ] = useState(false); function toggleItem() { setExpanded(!expanded); } const body = <View style={styles.accordBody}>{ children }</View>; return ( <View style={styles.accordContainer}> <TouchableOpacity style={styles.accordHeader} onPress={ toggleItem }> <Text style={styles.accordTitle}>{ title }</Text> <Icon name={ expanded ? 'chevron-up' : 'chevron-down' } size={20} color="#bbb" /> </TouchableOpacity> { expanded && body } </View> );
} function App(): JSX.Element { return ( <SafeAreaView style={styles.container}> <ScrollView contentInsetAdjustmentBehavior="automatic" style={styles.container}> <AccordionItem title="Native development"> <Text style={styles.textSmall}>React Native lets you create truly native apps and doesn't compromise your users' experiences. It provides a core set of platform agnostic native components </Text> </AccordionItem> <AccordionItem title="Fast refresh"> <Text style={styles.textSmall}>See your changes as soon as you save. With the power of JavaScript, React Native lets you iterate at lightning speed.</Text> </AccordionItem> <AccordionItem title="Cross-platform"> <Text style={styles.textSmall}>React components wrap existing native code and interact with native APIs via React’s declarative UI paradigm and JavaScript. This enables native app development for whole new teams of developers</Text> <View style={styles.seperator}></View> <Button title="See more..."/> </AccordionItem> </ScrollView> </SafeAreaView> );
} const styles = StyleSheet.create({ container: { flex: 1 }, accordContainer: { paddingBottom: 4 }, accordHeader: { padding: 12, backgroundColor: '#666', color: '#eee', flex: 1, flexDirection: 'row', justifyContent:'space-between' }, accordTitle: { fontSize: 20, }, accordBody: { padding: 12 }, textSmall: { fontSize: 16 }, seperator: { height: 12 }
}); export default App;

Run the above code. You’ll see a sample accordion, as shown in the following preview:

<img data-attachment-id="162661" data-permalink="https://blog.logrocket.com/building-react-native-collapsible-accordions/attachment/accordion-without-expanding-collapsing-animations/" data-orig-file="https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions.gif" data-orig-size="730,1622" data-comments-opened="1" data-image-meta="{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0"}" data-image-title="accordion-without-expanding-collapsing-animations" data-image-description data-image-caption="

An accordion without expanding/collapsing animations” data-medium-file=”https://blog.logrocket.com/wp-content/uploads/2023/03/accordion-without-expanding-collapsing-animations-135×300.gif” data-large-file=”https://blog.logrocket.com/wp-content/uploads/2023/03/accordion-without-expanding-collapsing-animations-461×1024.gif” decoding=”async” class=”size-full wp-image-162661″ src=”https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions.gif” alt=”An accordion without expanding/collapsing animations” width=”730″ height=”1622″>

An accordion without expanding/collapsing animations

The sample accordion plays a flashing animation when we press each header because of the TouchableOpacity component, but we don’t have any user-friendly expanding/collapsing animation for the accordion panel — the accordion body appears and disappears instantly.

Try to add more styles and dynamically change them based on the expanded state field. We’ll add expanding/collapsing animations soon!

Creating an animated accordion

React Native offers two inbuilt APIs for handling animations: Animated and LayoutAnimation. We’ll use LayoutAnimation here because we need to animate the accordion body in the render cycle based on the expanded state field’s boolean value.

Turning on animations for the accordion is so easy with LayoutAnimation. First, update your react-native imports as follows:

import { SafeAreaView, ScrollView, StyleSheet, Text, TouchableOpacity, View, Button, Platform, UIManager, LayoutAnimation
} from 'react-native';

Next, add the following code snippet right after all the import statements to enable LayoutAnimation on Android:

if(Platform.OS === 'android') { UIManager.setLayoutAnimationEnabledExperimental(true);
}

Finally, let React Native animate the accordion body when it renders by using the following toggleItem implementation:

function toggleItem() { LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut); setExpanded(!expanded);
}

Look at the live app and press the accordion headers. You will see expanding/collapsing animations for the accordion:

<img data-attachment-id="162664" data-permalink="https://blog.logrocket.com/building-react-native-collapsible-accordions/attachment/accordion-with-expanding-collapsing-animations/" data-orig-file="https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-1.gif" data-orig-size="730,1622" data-comments-opened="1" data-image-meta="{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0"}" data-image-title="accordion-with-expanding-collapsing-animations" data-image-description data-image-caption="

An accordion with expanding/collapsing animations” data-medium-file=”https://blog.logrocket.com/wp-content/uploads/2023/03/accordion-with-expanding-collapsing-animations-135×300.gif” data-large-file=”https://blog.logrocket.com/wp-content/uploads/2023/03/accordion-with-expanding-collapsing-animations-461×1024.gif” decoding=”async” loading=”lazy” class=”size-full wp-image-162664″ src=”https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-1.gif” alt=”An accordion with expanding/collapsing animations” width=”730″ height=”1622″>

An accordion with expanding/collapsing animations

You can change this animation by using a different preset or creating a custom animation with the LayoutAnimation.create method. Look at the following example:

LayoutAnimation.configureNext(LayoutAnimation.create(500, 'linear', 'scaleY'));

The above configuration plays the following animations:

<img data-attachment-id="162666" data-permalink="https://blog.logrocket.com/building-react-native-collapsible-accordions/attachment/using-custom-animation-accordion/" data-orig-file="https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-2.gif" data-orig-size="730,1622" data-comments-opened="1" data-image-meta="{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0"}" data-image-title="using-custom-animation-accordion" data-image-description data-image-caption="

Using a custom animation for the accordion” data-medium-file=”https://blog.logrocket.com/wp-content/uploads/2023/03/using-custom-animation-accordion-135×300.gif” data-large-file=”https://blog.logrocket.com/wp-content/uploads/2023/03/using-custom-animation-accordion-461×1024.gif” decoding=”async” loading=”lazy” class=”size-full wp-image-162666″ src=”https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-2.gif” alt=”Using a custom animation for the accordion” width=”730″ height=”1622″>

Using a custom animation for the accordion

Selectively enabling collapsible sections

You can choose any development strategy to build accordions from scratch based on your design requirements. For example, assume that you want to activate only one collapsible section at a time in a dynamic accordion. Then, you can make AccordionItem stateless and create a new stateful Accordion component to handle dynamic accordion items.

Use the following code segment in your App.tsx file:

type AccordionItemPros = PropsWithChildren<{ title: string;
}>; type AccordionData = { title: string; content: JSX.Element; expanded: boolean; onHeaderPress: (index: number) => void;
}; type AccordionProps = PropsWithChildren<{ data: AccordionData;
}>; function AccordionItem({ children, title, expanded, onHeaderPress }: AccordionItemPros): JSX.Element { const body = <View style={styles.accordBody}>{ children }</View>; return ( <View style={styles.accordContainer}> <TouchableOpacity style={styles.accordHeader} onPress={ onHeaderPress }> <Text style={styles.accordTitle}>{ title }</Text> <Icon name={ expanded ? 'chevron-up' : 'chevron-down' } size={20} color="#bbb" /> </TouchableOpacity> { expanded && body } </View> );
} function Accordion({ data }: AccordionProps): JSX.Element { const [ expandedIndex, setExpandedIndex ] = useState(null); function handleHeaderPress(index) { LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut); setExpandedIndex(expandedIndex === index ? null : index); } return( <> { data.map((item, index) => ( <AccordionItem key={index} title={item.title} expanded={expandedIndex === index} onHeaderPress={ () => handleHeaderPress(index) }> {item.content} </AccordionItem> )) } </> );
} function App(): JSX.Element { return ( <SafeAreaView style={styles.container}> <ScrollView contentInsetAdjustmentBehavior="automatic" style={styles.container}> <Accordion data={[ { title: 'Native development', content: <Text style={styles.textSmall}> React Native lets you create truly native apps and doesn't compromise your users' experiences. It provides a core set of platform agnostic native components </Text> }, { title: 'Fast refresh', content: <Text style={styles.textSmall}> See your changes as soon as you save. With the power of JavaScript, React Native lets you iterate at lightning speed. </Text> }, { title: 'Cross-platform', content: <><Text style={styles.textSmall}>React components wrap existing native code and interact with native APIs via React’s declarative UI paradigm and JavaScript. This enables native app development for whole new teams of developers</Text> <View style={styles.seperator}></View> <Button title="See more..."/> </> } ] }/> </ScrollView> </SafeAreaView> );
}

Here, the Accordion component accepts a data prop and lets you define accordion items dynamically, so it’s possible to use this accordion easily with RESTful API data. The handleHeaderPress function enables layout animations and sets the currently active collapsible section based on the collapsible panel’s indices.

Once you run the updated app, you will see the following result:

<img data-attachment-id="162669" data-permalink="https://blog.logrocket.com/building-react-native-collapsible-accordions/attachment/opening-one-accordion-panel/" data-orig-file="https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-3.gif" data-orig-size="730,1622" data-comments-opened="1" data-image-meta="{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0"}" data-image-title="opening-one-accordion-panel" data-image-description data-image-caption="

Opening one accordion panel at a time” data-medium-file=”https://blog.logrocket.com/wp-content/uploads/2023/03/opening-one-accordion-panel-135×300.gif” data-large-file=”https://blog.logrocket.com/wp-content/uploads/2023/03/opening-one-accordion-panel-461×1024.gif” decoding=”async” loading=”lazy” class=”size-full wp-image-162669″ src=”https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-3.gif” alt=”Opening one accordion panel at a time” width=”730″ height=”1622″>

Opening one accordion panel at a time

You can auto-open the first collapsible section as follows:

const [ expandedIndex, setExpandedIndex ] = useState(0);

Developing an accordion with react-native-collapsible

Earlier, we built an accordion from scratch with React Native’s inbuilt components, Font Awesome vector icons, and the inbuilt animation APIs. Even though we implemented the accordion from scratch, it didn’t take much time since our design requirements weren’t complex.

If we need to implement more features for the accordion or have very limited development time, using a third-party accordion library is a good idea.

The react-native-collapsible library offers a pre-developed, customizable accordion component. Let’s make an accordion with react-native-collapsible!

Create a new React Native project with the following command:

npx react-native init AccordionFromLibrary
cd AccordionFromLibrary

Install the required dependencies as follows:

npm install react-native-collapsible react-native-vector-icons
# --- or ---
yarn add react-native-collapsible react-native-vector-icons

Make sure to configure the project for the react-native-vector-icons package according to the official installation guide.

Now, add the following code to your App.tsx file:

import React, { useState } from 'react';
import type { PropsWithChildren } from 'react';
import { SafeAreaView, ScrollView, StyleSheet, Text, View, Button,
} from 'react-native';
import Icon from 'react-native-vector-icons/FontAwesome';
import Accordion from 'react-native-collapsible/Accordion'; function App(): JSX.Element { const [ activeSections, setActiveSections ] = useState([]); const sections = [ { title: 'Native development', content: <Text style={styles.textSmall}> React Native lets you create truly native apps and doesn't compromise your users' experiences. It provides a core set of platform agnostic native components </Text> }, { title: 'Fast refresh', content: <Text style={styles.textSmall}> See your changes as soon as you save. With the power of JavaScript, React Native lets you iterate at lightning speed. </Text> }, { title: 'Cross-platform', content: <><Text style={styles.textSmall}>React components wrap existing native code and interact with native APIs via React's declarative UI paradigm and JavaScript. This enables native app development for whole new teams of developers</Text> <View style={styles.seperator}></View> <Button title="See more..."/> </> } ]; function renderHeader(section, _, isActive) { return ( <View style={styles.accordHeader}> <Text style={styles.accordTitle}>{ section.title }</Text> <Icon name={ isActive ? 'chevron-up' : 'chevron-down' } size={20} color="#bbb" /> </View> ); }; function renderContent(section, _, isActive) { return ( <View style={styles.accordBody}> {section.content} </View> ); } return ( <SafeAreaView style={styles.container}> <ScrollView contentInsetAdjustmentBehavior="automatic" style={styles.container}> <Accordion align="bottom" sections={sections} activeSections={activeSections} renderHeader={renderHeader} renderContent={renderContent} onChange={(sections) => setActiveSections(sections)} sectionContainerStyle={styles.accordContainer} /> </ScrollView> </SafeAreaView> );
} const styles = StyleSheet.create({ container: { flex: 1 }, accordContainer: { paddingBottom: 4 }, accordHeader: { padding: 12, backgroundColor: '#666', color: '#eee', flex: 1, flexDirection: 'row', justifyContent:'space-between' }, accordTitle: { fontSize: 20, }, accordBody: { padding: 12 }, textSmall: { fontSize: 16 }, seperator: { height: 12 }
}); export default App;

The above source file renders an accordion by importing the Accordion component from the react-native-collapsible library. The above source code produces the following result:

<img data-attachment-id="162672" data-permalink="https://blog.logrocket.com/building-react-native-collapsible-accordions/attachment/accordion-created-with-library/" data-orig-file="https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-4.gif" data-orig-size="730,1622" data-comments-opened="1" data-image-meta="{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0"}" data-image-title="accordion-created-with-library" data-image-description data-image-caption="

An accordion created with a library” data-medium-file=”https://blog.logrocket.com/wp-content/uploads/2023/03/accordion-created-with-library-135×300.gif” data-large-file=”https://blog.logrocket.com/wp-content/uploads/2023/03/accordion-created-with-library-461×1024.gif” decoding=”async” loading=”lazy” class=”size-full wp-image-162672″ src=”https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-4.gif” alt=”An accordion created with a library” width=”730″ height=”1622″>

An accordion created with a library

The pre-developed Accordion component is so flexible — see how it lets developers build accordions with custom styles and content? For example, we added an icon for the accordion header with the renderHeader function:

function renderHeader(section, _, isActive) { return ( <View style={styles.accordHeader}> <Text style={styles.accordTitle}>{ section.title }</Text> <Icon name={ isActive ? 'chevron-up' : 'chevron-down' } size={20} color="#bbb" /> </View> ); };

This library lets you customize the accordion’s behavior with several props, just as it offers you custom styles and flexible accordion content support. For example, you can customize the expanding/collapsing animation as follows:

<Accordion easing="easeInBounce" duration={600} .... ....
/>

The above setup renders a slow bouncing animation as follows:

<img data-attachment-id="162674" data-permalink="https://blog.logrocket.com/building-react-native-collapsible-accordions/attachment/using-bouncing-effect-library-based-accordion/" data-orig-file="https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-5.gif" data-orig-size="730,1622" data-comments-opened="1" data-image-meta="{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0"}" data-image-title="using-bouncing-effect-library-based-accordion" data-image-description data-image-caption="

Using a bouncing effect for the library-based accordion” data-medium-file=”https://blog.logrocket.com/wp-content/uploads/2023/03/using-bouncing-effect-library-based-accordion-135×300.gif” data-large-file=”https://blog.logrocket.com/wp-content/uploads/2023/03/using-bouncing-effect-library-based-accordion-461×1024.gif” decoding=”async” loading=”lazy” class=”size-full wp-image-162674″ src=”https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-5.gif” alt=”Using a bouncing effect for the library-based accordion” width=”730″ height=”1622″>

Using a bouncing effect for the library-based accordion

By default, the library opens one collapsible section at a time, but you can override it as follows:

<Accordion expandMultiple={true} .... ....
/>

Now, you can expand multiple collapsible sections, as shown in the following preview:

<img data-attachment-id="162676" data-permalink="https://blog.logrocket.com/building-react-native-collapsible-accordions/attachment/configuring-library-component-enable-multiple-accordion-panels/" data-orig-file="https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-6.gif" data-orig-size="730,1622" data-comments-opened="1" data-image-meta="{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0"}" data-image-title="configuring-library-component-enable-multiple-accordion-panels" data-image-description data-image-caption="

Configuring the library component to enable multiple accordion panels” data-medium-file=”https://blog.logrocket.com/wp-content/uploads/2023/03/configuring-library-component-enable-multiple-accordion-panels-135×300.gif” data-large-file=”https://blog.logrocket.com/wp-content/uploads/2023/03/configuring-library-component-enable-multiple-accordion-panels-461×1024.gif” decoding=”async” loading=”lazy” class=”size-full wp-image-162676″ src=”https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-6.gif” alt=”Configuring the library component to enable multiple accordion panels” width=”730″ height=”1622″>

Configuring the library component to enable multiple accordion panels

See all supported accordion props from the official documentation.

Developing an accordion with accordion-collapse-react-native

The React Native library ecosystem is so competitive — we can find alternatives for almost all React Native packages. The accordion-collapse-react-native library is an alternative to react-native-collapsible. This library offers you a pre-developed, customizable AccordionList component for creating accordions. Let’s use this library too!

We can use the previous project to check out this library. Install the library with the following command:

npm install accordion-collapse-react-native
#--- or ---
yarn add accordion-collapse-react-native

Add the following code to your App.tsx file:

import React from 'react';
import { SafeAreaView, ScrollView, StyleSheet, Text, View, Button, Platform, UIManager, LayoutAnimation
} from 'react-native';
import Icon from 'react-native-vector-icons/FontAwesome';
import { AccordionList } from 'accordion-collapse-react-native'; if(Platform.OS === 'android') { UIManager.setLayoutAnimationEnabledExperimental(true);
} function App(): JSX.Element { const sections = [ { title: 'Native development', content: <Text style={styles.textSmall}> React Native lets you create truly native apps and doesn't compromise your users' experiences. It provides a core set of platform agnostic native components </Text> }, { title: 'Fast refresh', content: <Text style={styles.textSmall}> See your changes as soon as you save. With the power of JavaScript, React Native lets you iterate at lightning speed. </Text> }, { title: 'Cross-platform', content: <><Text style={styles.textSmall}>React components wrap existing native code and interact with native APIs via React's declarative UI paradigm and JavaScript. This enables native app development for whole new teams of developers</Text> <View style={styles.seperator}></View> <Button title="See more..."/> </> } ]; function renderHeader(section, _, isActive) { return ( <View style={styles.accordHeader}> <Text style={styles.accordTitle}>{ section.title }</Text> <Icon name={ isActive ? 'chevron-up' : 'chevron-down' } size={20} color="#bbb" /> </View> ); }; function renderContent(section, _, isActive) { return ( <View style={styles.accordBody}> {section.content} </View> ); } function handleOnToggle() { LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut); } return ( <SafeAreaView style={styles.container}> <AccordionList list={sections} header={renderHeader} body={renderContent} onToggle={handleOnToggle} /> </SafeAreaView> );
} const styles = StyleSheet.create({ container: { flex: 1 }, accordHeader: { padding: 12, backgroundColor: '#666', color: '#eee', flex: 1, flexDirection: 'row', justifyContent:'space-between', marginBottom: 4 }, accordTitle: { fontSize: 20, }, accordBody: { padding: 12 }, textSmall: { fontSize: 16 }, seperator: { height: 12 }
}); export default App;

Here, we created an accordion with the pre-developed AccordionList component from the accordion-collapse-react-native library. AccordionList offers a flexible way to use content and header, just like react-native-collapsible‘s Accordion, but it comes with fewer customizability options compared to Accordion. Also, AccordionList doesn’t come with inbuilt animations, so we’ve used LayoutAnimation in the above code.

Run the above code. You will see the same accordion we built earlier, but with an alternative library:

<img data-attachment-id="162679" data-permalink="https://blog.logrocket.com/building-react-native-collapsible-accordions/attachment/accordion-created-with-alternative-accordion-library/" data-orig-file="https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-7.gif" data-orig-size="730,1622" data-comments-opened="1" data-image-meta="{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0"}" data-image-title="accordion-created-with-alternative-accordion-library" data-image-description data-image-caption="

An accordion created with an alternative accordion library” data-medium-file=”https://blog.logrocket.com/wp-content/uploads/2023/03/accordion-created-with-alternative-accordion-library-135×300.gif” data-large-file=”https://blog.logrocket.com/wp-content/uploads/2023/03/accordion-created-with-alternative-accordion-library-461×1024.gif” decoding=”async” loading=”lazy” class=”size-full wp-image-162679″ src=”https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-7.gif” alt=”An accordion created with an alternative accordion library” width=”730″ height=”1622″>

An accordion created with an alternative accordion library

See all supported props from the official documentation.

Using accordion components from UI kits

Some developers use inbuilt React Native framework components and third-party components from various npm packages. For example, earlier, we used react-native-collapsible with its inbuilt components, such as Button, View, etc. Other developers stick to one UI kit, rather than using various components from various npm packages. This second approach brings you the following benefits:

  • You don’t need to worry about active maintenance or unsupported features of multiple npm packages because UI kits are usually well-maintained and fully featured projects
  • A UI kit typically consists of components that follow consistent UI/UX guidelines, whereas multiple libraries may follow different or conflicting UX practices

If you use a UI kit that offers a pre-included accordion component, you don’t need to implement an accordion yourself or import one from a third-party package.

Look at the following accordion components that we can import from popular UI kits.

Accordion from MUI

<img data-attachment-id="162681" data-permalink="https://blog.logrocket.com/building-react-native-collapsible-accordions/attachment/mui-accordion-preview/" data-orig-file="https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions.png" data-orig-size="730,360" data-comments-opened="1" data-image-meta="{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0"}" data-image-title="mui-accordion-preview" data-image-description data-image-caption="

MUI accordion preview” data-medium-file=”https://blog.logrocket.com/wp-content/uploads/2023/03/mui-accordion-preview-300×148.png” data-large-file=”https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions.png” decoding=”async” loading=”lazy” class=”size-full wp-image-162681″ src=”https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions.png” alt=”MUI accordion preview” width=”730″ height=”360″ srcset=”https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions.png 730w, https://blog.logrocket.com/wp-content/uploads/2023/03/mui-accordion-preview-300×148.png 300w” sizes=”(max-width: 730px) 100vw, 730px”>

MUI accordion preview

React Native MUI offers the pre-developed Accordion component to add Material Design-based accordions into your apps.

List.Accordion from React Native Paper

<img data-attachment-id="162684" data-permalink="https://blog.logrocket.com/building-react-native-collapsible-accordions/attachment/react-native-paper-list-accordion-preview/" data-orig-file="https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-1.png" data-orig-size="730,322" data-comments-opened="1" data-image-meta="{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0"}" data-image-title="react-native-paper-list-accordion-preview" data-image-description data-image-caption="

React Native Paper’s list.accordion preview” data-medium-file=”https://blog.logrocket.com/wp-content/uploads/2023/03/react-native-paper-list-accordion-preview-300×132.png” data-large-file=”https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-1.png” decoding=”async” loading=”lazy” class=”size-full wp-image-162684″ src=”https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-1.png” alt=”React Native Paper’s list.accordion preview” width=”730″ height=”322″ srcset=”https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-1.png 730w, https://blog.logrocket.com/wp-content/uploads/2023/03/react-native-paper-list-accordion-preview-300×132.png 300w” sizes=”(max-width: 730px) 100vw, 730px”>

React Native Paper’s list.accordion preview

React Native Paper offers the List.Accordion component for creating list-based accordions with inbuilt expand/collapse icons.

Accordion from Ant Design

<img data-attachment-id="162691" data-permalink="https://blog.logrocket.com/building-react-native-collapsible-accordions/attachment/ant-design-accordion-preview/" data-orig-file="https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-2.png" data-orig-size="730,793" data-comments-opened="1" data-image-meta="{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0"}" data-image-title="ant-design-accordion-preview" data-image-description data-image-caption="

Ant Design’s accordion preview” data-medium-file=”https://blog.logrocket.com/wp-content/uploads/2023/03/ant-design-accordion-preview-276×300.png” data-large-file=”https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-2.png” decoding=”async” loading=”lazy” class=”size-full wp-image-162691″ src=”https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-2.png” alt=”Ant Design’s accordion preview” width=”730″ height=”793″ srcset=”https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-2.png 730w, https://blog.logrocket.com/wp-content/uploads/2023/03/ant-design-accordion-preview-276×300.png 276w” sizes=”(max-width: 730px) 100vw, 730px”>

Ant Design’s accordion preview

According to Ant Design’s UX rules, it offers the Accordion component to group, show, and hide complex app areas.

ListItem.Accordion from React Native Elements

<img data-attachment-id="162695" data-permalink="https://blog.logrocket.com/building-react-native-collapsible-accordions/attachment/react-native-element-listitem-accordion-preview/" data-orig-file="https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-3.png" data-orig-size="730,823" data-comments-opened="1" data-image-meta="{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0"}" data-image-title="react-native-element-listitem-accordion-preview" data-image-description data-image-caption="

React Native Element’s ListItem.Accordion preview” data-medium-file=”https://blog.logrocket.com/wp-content/uploads/2023/03/react-native-element-listitem-accordion-preview-266×300.png” data-large-file=”https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-3.png” decoding=”async” loading=”lazy” class=”size-full wp-image-162695″ src=”https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-3.png” alt=”React Native Element’s listitem.accordion preview” width=”730″ height=”823″ srcset=”https://technobabble.com.au/wp-content/uploads/2023/03/options-for-building-react-native-collapsible-accordions-3.png 730w, https://blog.logrocket.com/wp-content/uploads/2023/03/react-native-element-listitem-accordion-preview-266×300.png 266w” sizes=”(max-width: 730px) 100vw, 730px”>

React Native Element’s ListItem.Accordion preview

React Native Elements also offer a customizable, pre-developed accordion via the ListItem.Accordion component.

Conclusion

In this tutorial, we’ve discussed the available options for creating accordions in React Native apps. We’ve built accordions from scratch, and used pre-developed accordions from two popular libraries. Also, we’ve discussed available accordion components in popular React Native UI kits.

Like any other development approach, the best accordion development method depends on your design requirements, delivery timeline, and development team’s skills. However, you should always adhere to generally-accepted UI/UX principles while developing accordions, such as using suitable expand/collapse icons, playing smooth minimal animations, and grouping content accordingly.

The post Options for building React Native collapsible accordions appeared first on LogRocket Blog.

from LogRocket Blog https://ift.tt/PlcZjx1
Gain $200 in a week
via Read more

Similar Posts