为什么反应本机中的部分列表不返回正确的行数据?



这是我要在部分列表中呈现的数据

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
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>
);
}}
/>

最初基于该条件的cnt = 0我渲染第一个按钮后,我根据uniqueID正确增加计数并重新渲染组件。同样,当我按下新渲染的按钮时,它会返回不同的uniqueID。有没有解决这个问题的解决方案。提前谢谢。

我想通了,因为我没有在函数中传递event参数incrementItem我只传递了两个参数,但我不知道为什么如果我们甚至传递两个参数,函数需要三个参数,就会调用函数。

最新更新