我有这三个文件:App,Main和Login。我正在尝试将用户名从登录传递到Main(然后传递给其他一些文件),但我未定义。 在应用程序中我有这些导航器:
const mainOptions = SwitchNavigator({
main : Main,
favPlaces : favPlaces,
})
const introStack = StackNavigator({
loggingIn : LoggingIn,
register : Register
})
const SwNavigator = SwitchNavigator({
login : introStack,
main : mainOptions
})
登录:
constructor(props){
super(props);
this.state={
credentials: {
username: "",
password:"",
}
}
this.navigate = this.props.navigation.navigate;
}
signIn(){ //when a button is clicked
{....}
this.navigate("main", {
username: this.state.username,
});
}
}
主要:
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); //UNDEFINED