Give this a shot:
MainComponent.tsx
import React, { useState } from 'react';
import { View, Pressable, StyleSheet, Text, FlatList } from 'react-native';
import { AnimateHeight } from './animated-height';
import { Ionicons } from '@expo/vector-icons';
import { MotiView } from 'moti';
import Constants from 'expo-constants';
export default function MainComponent() {
const questionsData = [
{
id: 1,
question: "Are you a banana?",
desc: "No, I am not.",
},
{
id: 2,
question: "Are you an apple?",
desc: "Lorem Ipsum is simply dummy text of the printing and typesetting industry... etc.",
},
]
const [currentIndex, setCurrentIndex] = useState(0);
const [showAnswers, toggleShowAnswers] = React.useReducer((show) => !show, false);
const renderQuestionItem = ({item, index}:any) => {
return(
<View style={styles.screen}>
<View style={styles.itemContainer}>
<Pressable onPress={() => setCurrentIndex(index)} style={styles.questionContainer}>
<Text selectable={false} style={styles.questionText}>
{item?.question}
</Text>
<MotiView
animate={{rotateZ: currentIndex == index ? '-90deg' : '0deg',}}>
<Ionicons name="chevron-forward" color="white" size={17} />
</MotiView>
</Pressable>
<AnimateHeight enterFrom="bottom" hide={currentIndex == index ? showAnswers : !showAnswers}>
<View style={styles.answerContainer}>
<Text style={styles.answerText}>
{item?.desc}
</Text>
</View>
</AnimateHeight>
</View>
</View>
)
}
return (
<View>
<FlatList
data={questionsData}
renderItem={renderQuestionItem}
keyExtractor={(item)=> item.id.toString()}
/>
</View>
);
}
const styles = StyleSheet.create({
screen: {
flex: 1,
backgroundColor: '#161618',
paddingTop: Constants.statusBarHeight,
},
itemContainer: {
borderBottomWidth: 1,
borderBottomColor: '#232326',
},
questionContainer: {
padding: 16,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
},
answerContainer: {
padding: 16,
marginTop: -16
},
answerText: {
color: '#A09FA5',
lineHeight: 20
},
questionText: {
color: '#EDEDEE',
fontWeight: 'bold',
},
});