Search code examples
react-nativereact-native-elementsreact-native-sectionlist

Why section List in react native is not return correct row data?


This is my data to render in Section List

this.state = {
    data: [
    {
    title: "Popular Items",
    data: [
    {
    key: 1,
    name: "Briyani",
    image: "https://bootdey.com/img/Content/avatar/avatar6.png",
    uniqueID: 1,
    cnt: 0
    },
    {
    key: 2,
    name: "Chicken Lollypop",
    image: "https://bootdey.com/img/Content/avatar/avatar1.png",
    uniqueID: 2,
    cnt: 0
    },
    {
    key: 3,
    name: "Chicken kabab",
    image: "https://bootdey.com/img/Content/avatar/avatar7.png",
    uniqueID: 3,
    cnt: 0
    }]}]};

SectionList to Display

<SectionList
sections={this.state.data}
renderSectionHeader={({ section }) => {
return (
<View style={styles.titleContainer}>
<Text style={styles.title}>{section.title}</Text>
</View>
);
}}
keyExtractor={item => item.uniqueID.toString()}
extraData={this.state}
renderItem={({ item }) => {
return (
<View style={styles.container}>
{item.cnt === 0 ? (
<Button
title="Add"
onPress={e =>
this.incrementItem(e, item.uniqueID, item.cnt)
}
/>
) : (
<View>
<Button
title="-"
onPress={e =>
this.decrementItem(e, item.uniqueID, item.cnt)
}
/>
<Text>
{item.cnt}
</Text>
<Button
title="+"
onPress={() =>
this.incrementItem(item.uniqueID, item.cnt)
}
/>
</View>
)}
</View>
);
}}
/>

Initially the cnt = 0 based on that condition i render first button After i press i increment the count correctly based on uniqueID and re-render the component .Again when i Press on newly rendered Button it returns different uniqueID. Is there any solution to solve this. Thanks in advance.


Solution

  • I figured it out its because of i didn't pass event argument in incrementItem function i only pass two arguments but i didn't know why the function called if we even pass two arguments but function expects three params.