I'm adding a menu to every screen navigation header, so created a menu function that needs the navigation object to navigate to different screens based on the menu item tapped. But the navigation.navigate from props is undefined.
export default function GeneralMenu(props) {
const [showMenu, setShowMenu] = useState(false);
return (
<View style={{}}>
<Menu
visible={showMenu}
onDismiss={() => setShowMenu(false)}
anchor={
<TouchableOpacity onPress={() => setShowMenu(true)}>
<MaterialCommunityIcons
name="menu"
size={30}
style={{ color: 'white' }}
/>
</TouchableOpacity>
}>
<Menu.Item onPress={() => { console.log(props.navigation)}} title="Screen1" />
<Menu.Item onPress={() => {props.navigation.navigate('Screen1', {})}} title="Screen1" />
</Menu>
</View>
);
Then I use it like this:
return (
<Provider>
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="Home"
component={HomeComponent}
options={{ title: 'Home',
headerTitleAlign: 'center',
headerTintColor: '#fff',
headerStyle: {
backgroundColor: 'red'
},
headerRight: (navigation) => <GeneralMenu navigation={navigation} />,
}}
/>
...
...
</NavigationContainer>
</Provider>
);
The console.log() when first menu item is tapped shows:
Object {
"canGoBack": false,
"tintColor": "#fff",
}
As you see there is no navigate property. Please, any idea what I'm doing wrong?