Arrays 从平面列表中获取嵌套数组的键
我有一个平面列表,它正在接收具有以下数据结构的friendsArray 我有一个模态,它有一个onPress函数,有了这个onPress,我想得到这个键的值。我有以下代码,但通常这段代码为我提供了在平面列表中选择的任何内容的键。。。我想让所选项目的关键更深一层,我该怎么做 这里是App.jsArrays 从平面列表中获取嵌套数组的键,arrays,react-native,react-native-flatlist,flatlist,Arrays,React Native,React Native Flatlist,Flatlist,我有一个平面列表,它正在接收具有以下数据结构的friendsArray 我有一个模态,它有一个onPress函数,有了这个onPress,我想得到这个键的值。我有以下代码,但通常这段代码为我提供了在平面列表中选择的任何内容的键。。。我想让所选项目的关键更深一层,我该怎么做 这里是App.js onFriendChatPress = key => { let friend = this.state.friendsArray.find(game => { return game.
onFriendChatPress = key => {
let friend = this.state.friendsArray.find(game => { return game.key === key })
}
render(){
return{
<View>
<FriendsModal
onChatPress={() => this.onChatPress()}
onClosePress={() => this.closeModal()}
onFriendPress={() => this.onFriendPress()}
onFriendChatPress={() => this.onFriendChatPress()}
selGame={this.state.selGame}
visible={this.state.modalVisible}
selGame={this.state.selGame}
/>
</View>
}
onFriendChatPress=key=>{
让friend=this.state.friendsArray.find(游戏=>{return game.key===key})
}
render(){
返回{
this.onChatPress()}
onClosePress={()=>this.closeModal()}
onFriendPress={()=>this.onFriendPress()}
onFriendChatPress={()=>this.onFriendChatPress()}
selGame={this.state.selGame}
可见={this.state.modalVisible}
selGame={this.state.selGame}
/>
}
这是FriendsModal中的平面列表
<FlatList
style={styles.flatList}
data={this.props.selGame.friends}
horizontal={true}
renderItem={(info) => (
<ModalProfileCard
displayName={info.item.displayName}
image={info.item.image}
onFriendPress={() => this.props.onFriendPress(info.item.key)}
onFriendChatPress={() => this.props.onFriendChatPress(info.item.key)}
/>
)
}
/>
(
this.props.onFriendPress(info.item.key)}
onFriendChatPress={()=>this.props.onFriendChatPress(info.item.key)}
/>
)
}
/>
这是表格里的卡片
function modalProfileCard(props) {
return (
<View style={styles.container}>
<TouchableOpacity onPress={props.onFriendsPress} style={styles.friendInfo}>
<Image style={styles.profImage} source={{ uri: props.image }} />
<View style={styles.nameContainer}>
<Text style={styles.name}>{props.displayName}</Text>
</View>
</TouchableOpacity>
<TouchableOpacity style={styles.button} onPress={props.onFriendChatPress}>
{buttonText}
</TouchableOpacity>
</View >
)
}
功能modalProfileCard(道具){
返回(
{props.displayName}
{buttonText}
)
}
您可以这样做
const onPress = (key) => {
//you have the key here
}
return (
<Flatlist
data={your_data}
renderItem={({item})=> <YourComponent {...item} onPress={onPress} />}
/> )
const YourComponent = ({key,onPress}) => {
const onPress = () => {
onPress(key);
}
return (
<TouchableOpacity onPress={onPress}>
</TouchableOpacity>
)
}
const onPress=(键)=>{
//钥匙在这里
}
报税表(
}
/> )
constYourComponent=({key,onPress})=>{
const onPress=()=>{
按(键);
}
返回(
)
}
虽然我知道
在renderItem中,不应该使用info.displayName而不是info.item.displayNam吗?等等
还有一个家长公寓列表?您正在将所选游戏保存为selGame状态,对吗?您还可以保存它的索引,并可以传递给子组件,以便轻松找到所选游戏的密钥。共享您的平面列表代码,然后按打开位置modal@NooruddinLakhani-我已经添加了您要求的更多细节,有什么想法吗?在renderItem中,您的代码非常混乱,你不应该使用info.displayName而不是info.item.displayNam吗?@NooruddinLakhani-info.item.displayName有效,info.displayName未定义