TLDR: I have a Login screen where user logs in and navigates to second screen. There I want to open the menu and have the user name appear as text. How can I pass the user name to the menu? (DrawerNavigator).
Some samples of my code below.
Router:
import MyMenu from './MyMenu';
const MyStack = StackNavigator({
Login: {screen: Login},
Home: {screen: Home}
// more screens...
});
export const MyDrawer = DrawerNavigator({
Main: { screen: MyStack }
},
{ contentComponent: MyMenu }
);
Home Screen:
openMenu() {
this.props.navigation.navigate('DrawerOpen', userName);
}
MyMenu:
render() {
const userName = this.props.navigation.state.params;
return (
<View>
<Text>{userName}</Text>
// more stuff
</View>
);
}
I think this is what you want.
Then you can access drawer's navigation from your Stack navigation like below.
Alright, after spending so much time on this, I finally found a solution. Simply put, you can only send navigation props when navigating to a new screen - 'DrawerOpen' does not count for this.
To solve this I had to change my structure a little bit, instead of having the DrawerNavigator as the root drawer, I have a StackNavigator as the root drawer.
Before:
After:
Now in Login screen I navigate to Drawer like this:
And finally in MyMenu (which is part of 'Drawer'):
It works!
You need to pass props to your MyMenu Component:
Router:
Home Screen: