【发布时间】:2021-08-17 05:15:16
【问题描述】:
我是 React Native 应用程序开发的新手。我的问题是我也尝试使用数据(字符串和图像)导航到第一个屏幕到第二个屏幕。但会尝试导航错误。错误是未定义导航。请解决我的问题并编辑我的代码。我已附上我的代码供您参考。
import React from 'react';
import { StyleSheet, Text, View, FlatList, Image, TouchableOpacity } from 'react-native';
function Item({ item }) {
return (
<View style={styles.listItem}>
<Image source={{uri:item.photo}} style={{width:60, height:60,borderRadius:30}} />
<View style={{alignItems:"center",flex:1}}>
<Text style={{fontWeight:"bold"}}>{item.name}</Text>
<Text>{item.position}</Text>
</View>
<TouchableOpacity
onPress={() => getItem(item)}
style={{height:50,width:50, justifyContent:"center",alignItems:"center"}}>
<Text style={{color:"green"}}>Call</Text>
</TouchableOpacity>
</View>
);
}
const getItem = (item) => {
// Function for click on an item
alert('Id : ' + item.id + ' Title : ' + item.position);
};
const getpress=(item)=> {
this.props.navigation.navigate('SecondPage', {
itemId: item.name,
title: item.position.rendered,
});
}
export default class FirstPage extends React.Component {
state = {
data:[
{
"name": "Miyah Myles",
"email": "miyah.myles@gmail.com",
"position": "Data Entry Clerk",
"photo": "https:\/\/images.unsplash.com\/photo-1494790108377-be9c29b29330?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=200&fit=max&s=707b9c33066bf8808c934c8ab394dff6"
},
{
"name": "June Cha",
"email": "june.cha@gmail.com",
"position": "Sales Manager",
"photo": "https:\/\/randomuser.me\/api\/portraits\/women\/44.jpg"
}
]
}
render(){
//const { navigate } = this.props.navigation;
return (
<View style={styles.container}>
<TouchableOpacity onPress={()=>navigate('SecondPage')}>
<FlatList
style={{flex:1}}
data={this.state.data}
renderItem={({ item }) => <Item item={item}/>}
keyExtractor={item => item.email}
/>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F7F7F7',
marginTop:60
},
listItem:{
margin:10,
padding:10,
backgroundColor:"#FFF",
width:"80%",
flex:1,
alignSelf:"center",
flexDirection:"row",
borderRadius:5
}
});
【问题讨论】:
标签: javascript reactjs react-native onclick react-native-flatlist