我正在尝试显示一个 FlatList 项目,每个项目都包含一个相同按钮的 FlatList。单击其中一个按钮时,我希望同时接收父 FlatList 项和按钮对应的值。
我尝试了以下方法但没有成功(timeItem 未定义):
timeSlotsKeyExtractor = (item, index) => item.toString();
timeSlots =
[
'8:00', '10:00', '12:00', '14:00', '16:00', '18:00'
];
renderDate = ({ item }) => {
return (
<View>
<Text>{item.date}</Text>
<FlatList
data={this.timeSlots}
keyExtractor={this.timeSlotsKeyExtractor}
renderItem={({ timeItem }) => (
<TouchableOpacity onPress={() => this.addTimeSlot(item, timeItem)}>
<View><Text>{timeItem}</Text></View>
</TouchableOpacity>
)}
horizontal={true} />
</View>
)
}
addTimeSlot(item, timeItem) {
console.log(item)
console.log(timeItem)
}
我知道SectionLists,因此也试图使我的父FlatList的每个项目都成为一个包含项目和timeSlots数组的对象,但是,我不确定如何在renderItem部分中获取标题:
SectionList 代码截图:
<SectionList
sections={this.state.weekDates}
renderSectionHeader={({ section }) => (
<Text>{section.date} </Text>
)}
renderItem={({ item }) => (
<Text>
{iten.date}
{item.timeSlots}
</Text>
)}
keyExtractor={(item, index) => index}
/>
这些方法哪里出错了?我对 React Native 还很陌生,所以我很有可能错过了一些非常简单的东西。