这是我要在部分列表中呈现的数据
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
。有没有办法解决这个问题。提前致谢。