reactjs - 如何将父平面列表的索引值传递给子平面列表的 onPress 函数?
问题描述
如何将父 Flatlist 的索引值传递给子 Flatlist 的函数,以便我可以在函数中使用它并通过 setState 更改值。那么如何将父 Flatlist 的索引值传递给子 Flatlist 的函数,以便我可以在函数中使用它并通过 setState 更改值。
它以一个内置状态的列表开始
constructor(props) {
super(props);
this.state = {
list: [
{
question: 'il veicolo ha la gomma forata?',
answers: [
{
radioButtonValue: false,
text: strings.CONFIRMVIEW_yes,
},
{
radioButtonValue: true,
text: strings.CONFIRMVIEW_no,
},
],
},
]
}
}
checkBoxSelection = (item, index) => {
// if (item.radioButtonValue == index) {
// this.list.answers[index].radioButtonValue = true
// }
console.log(this.state.list[0].answers[index].radioButtonValue)
}
_renderItem = ({ item, index }) => {
return (
<View style={styles.cellView}>
<Text style={styles.questionText}>
{item.question.toUpperCase()}
</Text>
<FlatList
data={item.answers}
horizontal={true}
showsHorizontalScrollIndicator={true}
scrollEnabled={true}
bounces={false}
renderItem={this._renderItemOptions}
/>
{/* <View style={styles.yesNoRadioButtonView}>
<View style={styles.yesChekboxView}>
<TouchableOpacity onPress={ () => {console.log("TEST1"); this.checkBoxSelection()} }>
<Image source={(item.answers == index) ? AppImages.radioOn : AppImages.radioOff} style={styles.radioButton} />
</TouchableOpacity>
<Text style={styles.yesNoText}>
{strings.CONFIRMVIEW_yes}
</Text>
</View>
<View style={styles.noRadioButtonView}>
<TouchableOpacity onPress={() => { this.checkBoxSelection.bind(this) }}>
<Image source={(item.answers == 0) ? AppImages.radioOn : AppImages.radioOff} style={styles.radioButton} />
</TouchableOpacity>
<Text style={styles.yesNoText}>
{strings.CONFIRMVIEW_no}
</Text>
</View>
</View> */}
</View>
)
}
_renderItemOptions = ({ item, index }) => {
return (
<View>
<View style={styles.yesNoRadioButtonView}>
<TouchableOpacity onPress={() => { this.checkBoxSelection(item, index) }}>
<View style={styles.yesChekboxView}>
<Image
source={item.radioButtonValue ? AppImages.radioOn : AppImages.radioOff}
style={styles.radioButton}
/>
<Text style={styles.yesNoText}>
{item.text}
</Text>
</View>
</TouchableOpacity>
</View>
</View>
)
}
render() {
return (
<Container>
<Content style={styles.container}>
<FlatList
data={this.state.list}
showsVerticalScrollIndicator={false}
scrollEnabled={false}
bounces={false}
renderItem={this._renderItem}
/>
</Content>
<SafeAreaView>
<TouchableOpacity
style={[LayoutStyle.appBtn1]}
>
<Text style={[LayoutStyle.appBtnText1]}>
{strings.DAMAGE_SURVEY_comeOn.toUpperCase()}
</Text>
</TouchableOpacity>
</SafeAreaView>
</Container>
)
}
解决方案
在 Parent Flatlist 的 renderItem 函数中替换
renderItem = { () => {this.renderItemOptions(item, index)}}
和
renderItem={(childData) => this._renderItemOptions(childData, index)}
然后在子 Flatlist 的 renderItemOption 函数中需要使用另一个名称获取 parentIndex。
_renderItemOptions = ({ item, index }, parentIndex) => {
console.log("hello")
return (
<View>
<View style={styles.yesNoRadioButtonView}>
<TouchableOpacity onPress={() => { this.checkBoxSelection(item, index, parentIndex) }}>
<View style={styles.yesChekboxView}>
<Image
source={item.radioButtonValue ? AppImages.radioOn : AppImages.radioOff}
style={styles.radioButton}
/>
<Text style={styles.yesNoText}>
{item.text}
</Text>
</View>
</TouchableOpacity>
</View>
</View>
)
}
推荐阅读
- r - 如何在R中计算模糊性能指标和归一化分类熵
- javascript - 使用 innerHTML 和引号复制脚本
- jquery - 为什么 jquery 在 laravel 8 中无法使用路由参数?
- flutter - 如何为 XFile 类创建 Hive 适配器
- azure - 在 Azure PowerShell 函数中调用 WebClient.DownloadString 时出现“无效指针”异常
- rust - 展开选项并返回错误的最简单方法(如果没有)(无论如何)
- reactjs - WhatsApp 使用哪个用户代理/机器人来抓取网站以进行链接预览
- java - 添加扩展后,Java EE 8 应用程序无法在 Wildfly 25 上启动
- arrays - 使用 Haskell 的 N 维数组
- folium - 如何将谷歌栅格图层放入小部件?