Accordion in React Native

Sanjana Human In Tech
2 min readOct 18, 2023

--

Creating an accordion component in React Native is a common UI pattern, allowing you to display collapsible sections of content. You can achieve this by managing the state of each section and rendering them accordingly.

Here’s an example of how to create an accordion component in React Native:

Dummy Data to display :

menu : [
{ title : "Sizzler"
data : [{key: "Paneer Sizzler" , value : "false"},
{key: "Italian Sizzler", value :"false"}]
},
{ title : "Pizza"
data : [{key: "FarmHarvest Pizza" , value : "false"},
{key: "Veg Extravegneza", value :"false"}]
},
{ title : "Garlic Bread"
data : [{key: "Herbs Garlic Bread" , value : "false"},
{key: "Extra cheese Garlic Bread", value :"false"}]
},
]

import React, {Component} from 'react';
import { View, TouchableOpacity, Text, FlatList, StyleSheet, LayoutAnimation, Platform, UIManager} from "react-native";
import { Colors } from './Colors';
import Icon from "react-native-vector-icons/MaterialIcons";

export default class Accordian extends Component{

constructor(props) {
super(props);
this.state = {
data: props.data,
expanded : false,
}

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

render() {

return (
<View>
<TouchableOpacity style={styles.row} onPress={()=>this.toggleExpand()}>
<Text style={[styles.title]}>{this.props.title}</Text>
<Icon name={this.state.expanded ? 'keyboard-arrow-up' : 'keyboard-arrow-down'} size={30} color={Colors.DARKGRAY} />
</TouchableOpacity>
<View style={styles.parentHr}/>
{
this.state.expanded &&
<View style={{}}>
<FlatList
data={this.state.data}
numColumns={1}
scrollEnabled={false}
renderItem={({item, index}) =>
<View>
<TouchableOpacity style={[styles.childRow, styles.button, item.value ? styles.btnActive : styles.btnInActive]} onPress={()=>this.onClick(index)}>
<Text style={[styles.font, styles.itemInActive]} >{item.key}</Text>
<Icon name={'check-circle'} size={24} color={ item.value ? Colors.GREEN : Colors.LIGHTGRAY } />
</TouchableOpacity>
<View style={styles.childHr}/>
</View>
}/>
</View>
}

</View>
)
}

onClick=(index)=>{
const temp = this.state.data.slice()
temp[index].value = !temp[index].value
this.setState({data: temp})
}

toggleExpand=()=>{
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
this.setState({expanded : !this.state.expanded})
}

}

const styles = StyleSheet.create({
container:{
justifyContent: 'center',
alignItems: 'center'
},
button:{
width:'100%',
height:54,
alignItems:'center',
paddingLeft:35,
paddingRight:35,
fontSize: 12,
},
title:{
fontSize: 14,
fontWeight:'bold',
color: Colors.DARKGRAY,
},
itemActive:{
fontSize: 12,
color: Colors.GREEN,
},
itemInActive:{
fontSize: 12,
color: Colors.DARKGRAY,
},
btnActive:{
borderColor: Colors.GREEN,
},
btnInActive:{
borderColor: Colors.DARKGRAY,
},
row:{
flexDirection: 'row',
justifyContent:'space-between',
height:56,
paddingLeft:25,
paddingRight:18,
alignItems:'center',
backgroundColor: Colors.CGRAY,
},
childRow:{
flexDirection: 'row',
justifyContent:'space-between',
backgroundColor: Colors.GRAY,
},
parentHr:{
height:1,
color: Colors.WHITE,
width:'100%'
},
childHr:{
height:1,
backgroundColor: Colors.LIGHTGRAY,
width:'100%',
},
colorActive:{
borderColor: Colors.GREEN,
},
colorInActive:{
borderColor: Colors.DARKGRAY,
}

});

This example demonstrates a basic accordion with two sections. You can expand on this by adding more sections and styling as needed.

Thank you for reading this article! Don’t forget to clap 👏 .

If you have any queries related to ReactNative, I’m always happy to help you. You can reach me on LinkedIn and gmail.

Happy Learning🚀 Happy Coding💻.

--

--

Sanjana Human In Tech
Sanjana Human In Tech

Written by Sanjana Human In Tech

A React Native front-end enthusiast and dedicated development engineer, eager to expand knowledge on development techniques and collaborate with others.

No responses yet