1

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

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。有没有办法解决这个问题。提前致谢。

4

1 回答 1

-1

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

于 2019-11-27T09:31:56.777 回答