我正在尝试在两个文件之间传递用户名,但它仍未定义。我哪里错了?谢谢!
//登录文件
constructor(props){
super(props);
this.state={
credentials: {
username: "",
password:"",
}
}
this.navigate = this.props.navigation.navigate;
}
signIn(){
{....}
this.navigate("main", {
username: this.state.username,
});
}
}
在Main:
constructor(props) {
super(props);
this.navigate = this.props.navigation.navigate;
this.params = this.props.navigation.state.params;
}
{...}
render() {
console.log(this.params.username);
//here it's undefined
答案 0 :(得分:0)
您正在主要组件中错误地访问参数,您需要从this.props.navigation.state.params
访问,您会得到它
constructor(props) {
super(props);
this.navigate = this.props.navigation.navigate;
this.params = this.props.navigation.state.params;
}
{...}
render() {
console.log(this.props.navigation.state.params.username);
答案 1 :(得分:0)
在params
时使用navigate()
传递参数。
this.navigate({
routeName: 'main',
key: 'main',
params: {
username: this.state.username
}
});
要使用this.props.navigation.state.params.username
来访问它。
希望这会有所帮助!