I have a react-native app. And I try to toggle a List.Accorion. I have some cards. And on every card there is a icon. And if a user triggers the icon then the List.Accorion has to be shown or hidden. But the current situation is that if a user triggers the icon all the List.Accorions from every card will be hidden or shown. What not correct is.
So this is the code:
const [breakfastExpanded, setBreakfastExpanded] = useState(false);
const [showAccordion, setShowAccordion] = useState(false);
const toggleAccordion = () => {
setShowAccordion(!showAccordion);
};
<CategoryList
data={categoryList}
renderItem={({ item }) => {
return (
<>
<TouchableOpacity
onPress={() => navigation.navigate("groepen", { subcategories: item.id })}>
<Spacer position="top" size="large">
<CategoryInfoCard category={item} />
</Spacer>
</TouchableOpacity>
<View>
<ButtonDetail onPress={toggleAccordion}>
<AntDesign name="twitter" size={24} color="green" />
</ButtonDetail>
</View>
{showAccordion && (
<List.Accordion
title="Description"
left={(props) => <List.Icon {...props} icon="bird" />}
expanded={breakfastExpanded}
onPress={() => setBreakfastExpanded(!breakfastExpanded)}>
<List.Item title={item.description} />
</List.Accordion>
)}
</>
);
}}
keyExtractor={(item) => item.id}
/>
Question: how to hide/show the List.Accorion only where the icon is triggered?
I have it now like:
export const CategoryScreen = ({ navigation }) => {
const { loading, categoryList } = useContext(CategoryContext);
const [breakfastExpanded, setBreakfastExpanded] = useState(false);
const [showAccordion, setShowAccordion] = useState([]);
const toggleAccordion = (id) => {
if (showAccordion.includes(id)) {
console.log(showAccordion);
let UpdateIds = [...showAccordion];
UpdateIds = UpdateIds.filter((ID) => ID !== id);
setShowAccordion(UpdateIds);
} else {
let UpdateIds = [...showAccordion];
UpdateIds.push(id);
setShowAccordion(UpdateIds);
}
};
return (
<SafeArea>
{loading && (
<LoadingContainer>
<ActivityIndicator animating={true} color={MD2Colors.green200} />
</LoadingContainer>
)}
<Search />
<CategoryList
data={categoryList}
renderItem={({ item }) => {
return (
<>
<TouchableOpacity
onPress={() => navigation.navigate("groepen", { subcategories: item.id })}>
<Spacer position="top" size="large">
<CategoryInfoCard category={item} />
</Spacer>
</TouchableOpacity>
<View>
<ButtonDetail onPress={toggleAccordion(item.id)}>
<AntDesign name="twitter" size={24} color="green" />
</ButtonDetail>
</View>
{showAccordion.includes(item.id) && (
<List.Accordion
title="Description"
left={(props) => <List.Icon {...props} icon="bird" />}
expanded={breakfastExpanded}
onPress={() => setBreakfastExpanded(!breakfastExpanded)}>
<List.Item title={item.description} />
</List.Accordion>
)}
</>
);
}}
keyExtractor={(item) => item.id}
/>
</SafeArea>
);
};