I'm very new to react native. So the problem here now is I want to expand and collapse the view only one item at a time. Currently, every item expands and collapses simultaneously eventhough I only click at one item Any suggestions ? Below is my code and I have attached the screenshot of my screen.
export default function AppCollapsible({ data }: { data: any }) {
const [collapsed, setCollapsed] = useState(true);
const [multipeSelect, setMultipleSelect] = useState(false);
const isExpanded = () => {
if (collapsed) {
setCollapsed(false);
} else {
setCollapsed(true);
}
};
useEffect(() => {});
return (
<View style={styles.listContainer}>
<TouchableOpacity onPress={isExpanded}>
<View style={styles.list}>
<Text style={styles.accName}>Current / Savings</Text>
<Text style={styles.accValue}>MYR 40,000</Text>
<View style={styles.imageIcon}>
<Image
source={require('ngcc_poc/src/assets/icons/active/ChevronDown.png')}
/>
</View>
<Collapsible collapsed={collapsed} align="center">
<CurrentSavingsCard />
</Collapsible>
</View>
</TouchableOpacity>
<TouchableOpacity onPress={isExpanded}>
<View style={styles.list}>
<Text style={styles.accName}>Fixed Deposit</Text>
<Text style={styles.accValue}>MYR 180,000</Text>
<View style={styles.imageIcon}>
<Image
source={require('ngcc_poc/src/assets/icons/active/ChevronDown.png')}
/>
</View>
<Collapsible collapsed={collapsed} align="center">
<FixedDeposit />
</Collapsible>
</View>
</TouchableOpacity>
</View>
);
}
It's not a complete answer but as a source of inspiration as @laurenyz suggested you.
You can have a look at this. I extracted the state logic to a new component ToggleView