[英]Passing data to routes navigation React Native
I made a screen of a flatlist that displays a bunch of Doctors infos(name,location,Picture),when i click on a doctor view it navigates to his profil.我制作了一个平面列表的屏幕,显示一堆医生信息(姓名、位置、图片),当我点击医生视图时,它会导航到他的个人资料。 for the moment the profil screen is just blank, but i want to pass the params so i can just display them without re doing the api call.
目前,配置文件屏幕只是空白,但我想传递参数,这样我就可以在不重新调用 api 的情况下显示它们。 here is the code of the research screen where the flatlist is:
这是平面列表所在的研究屏幕的代码:
render(){
return (
<View style={styles.main_container}>
<FlatList
data={this.state.dataSource}
keyExtractor={item=> item.id.toString()}
renderItem= {({item})=> <MedItem Med={item} />} />
</View>
);
}
i created a component custom med item to style my flat list: //Meditem.js我创建了一个组件自定义 med 项目来设计我的平面列表://Meditem.js
class MedItem extends React.Component {
render(){
const Med = this.props.Med
return (
<View style={styles.main_container} >
<View style={styles.ctr1}>
<TouchableOpacity onPress={()=>NavigationService.navigate('MedProfil')}>
<Image style={styles.img} source={require('../assets/Title.jpg')} />
</TouchableOpacity>
<TouchableOpacity onPress={()=>NavigationService.navigate('MedProfil')}>
<Text style={styles.txt}> {Med.name} </Text>
<Text style={{flexWrap:'wrap'}} > {Med.specialite} </Text>
<Text> {Med.work} </Text>
</TouchableOpacity>
</View>
<View style={styles.ctr2}>
<Text style={{textAlign:'center',marginBottom:5}}>Disponibilité</Text>
<Calendar/>
</View>
</View>
);
}
}
last this is my doctor profil screen:最后这是我的医生简介屏幕:
export default class MedProfilScreen extends React.Component {
render(){
return(
<View>
<Text>{Med.name}</Text>
<Button title='Prendre rendez-vous' onPress={()=>{}} />
</View>
);
}
}
Can someone please help me doing this.有人可以帮我做这个。 thank you
谢谢你
Send Data through navigation like this通过这样的导航发送数据
this.props.navigation.navigate("MedProfil",
{
name:Med.name
})
Get data in MedProfil Screen在 MedProfil Screen 中获取数据
const name=this.props.navigation.getParam("name")
In your Sending component use this:在您的发送组件中使用这个:
constructor(props) {
super(props);
this.navigate = this.props.navigation.navigate;
}
//Send it like this
this.navigate("MedProfilScreen", { name:Med.name });
In your receiving component在您的接收组件中
constructor(props) {
super(props);
this.params = this.props.navigation.state.params;
}
//Can access it using
console.log(this.params.name);
thank you everyone!谢谢大家! getParam won't work for me.
getParam 对我不起作用。 i used this to solve the problem
我用这个来解决问题
//MedItem.js
<TouchableOpacity onPress={()=>NavigationService.navigate('MedProfil',Med)}>
and in the profil screen //MedProfil.js并在配置文件屏幕 //MedProfil.js
export default function MedProfilScreen({route}){
const {name,specialite,work}=route.params;
return(
<ScrollView contentContainerStyle={{flex:1,alignItems:'center'}}>
<View style={styles.ctr1}>
<Text>{name}</Text>
<Text>{specialite}</Text>
<Text>{work}</Text>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.