I'm trying to create a menu button in the header area. However, I don't know how to get the onPress navigation to work. I added a drawer navigator using React Navigation 6.x.
I want to show drawer when i press on Menu Icon. the problem I am having is that props.navigation.openDrawer() does not work
https://source--react-navigation-docs.netlify.app/docs/drawer-based-navigation
App.tsx
import 'react-native-gesture-handler';
import * as React from 'react';
import {NavigationContainer} from '@react-navigation/native';
import{createDrawerNavigator} from '@react-navigation/drawer'
import {createNativeStackNavigator} from '@react-navigation/native-stack';
import Main from './src/screens/main';
const Stack = createNativeStackNavigator();
const Drawer = createDrawerNavigator();
const MyDrawer = () =>{
return (
<NavigationContainer>
<Drawer.Navigator initialRouteName="Main"
screenOptions={{
headerShown: false
}}
>
<Drawer.Screen
name="Main"
component={Main}
/>
</Drawer.Navigator>
</NavigationContainer>
);
};
export default MyDrawer;
src-> components->header->my_header.js
import MCIcon from 'react-native-vector-icons/MaterialCommunityIcons'
import MIcon from 'react-native-vector-icons/MaterialIcons'
import { useNavigation } from '@react-navigation-hooks';
const My_Header = (props) => {
const {navigate,goBack, toggleDrawer} = useNavigation();
const Main_Header =()=>{
return(
<Header style={styles.bg_red} androidStatusBarColor="#ef394e">
<Right style={styles.row}>
{
props.head_page_name?
<Text style={styles.texth2}>
{props.head_page_name}
</Text>
:
<Text style={styles.texth1}>
</Text>
}
{
props.right_btn=='back'?
<Ripple style={styles.btn} onPress={()=>goBack(null)} >
<MIcon name='arrow-back' style={[styles.icon,{rotation:180}]} />
</Ripple>
:
<Ripple style={styles.btn} onPress={()=>toggleDrawer()} >
<MCIcon name='menu' style={styles.icon} />
</Ripple>
}
</Right>
</Header>
)
}
I want to replace
<Ripple style={styles.btn} onPress={()=>toggleDrawer()} >
to
<Ripple style={styles.btn} onPress={()=>props.navigation.openDrawer()} >
But it doesn't work, Can you please help me how can convert code .
I changed the code, but still when I click on menu icon doesn't show anything.Thank you for your attention
src->screens->DrawerContent
import {StyleSheet, Text, View} from 'react-native';
import React from 'react';
import {
DrawerContentScrollView,
DrawerItemList,
DrawerItem,
} from '@react-navigation/drawer';
const DrawerContent = (props)=>{
const {navigation} = props;
return(
<DrawerContentScrollView {...props}>
<View>
<Text onPress={() => navigation.navigate('Main')}>
Home
</Text>
</View>
</DrawerContentScrollView>
);
};
export default React.memo(DrawerContent)
------------------------------------------------------
App.tsx
import 'react-native-gesture-handler';
import * as React from 'react';
import {NavigationContainer} from '@react-navigation/native';
import{createDrawerNavigator} from '@react-navigation/drawer'
import {createNativeStackNavigator} from '@react-navigation/native-stack';
import Main from './src/screens/main';
import DrawerContent from './src/screens/DrawerContent';
const Stack = createNativeStackNavigator();
const Drawer = createDrawerNavigator();
const MyStack = () => {
return (
<NavigationContainer>
<Stack.Navigator
initialRouteName="Main"
screenOptions={{
headerShown: false
}}
>
<Stack.Screen
name="Main"
component={Main}
/>
</Stack.Navigator>
</NavigationContainer>
);
};
const MyDrawer = () =>{
return (
<NavigationContainer>
<Drawer.Navigator initialRouteName="Main"
screenOptions={{
headerShown: false }}
drawerContent={props => <DrawerContent {...props} />}>
<Drawer.Screen
name="Main"
component={Main}
/>
-------------------------------------------------------------
src->component->header->my_header.js
import React from 'react'
import {ScrollView,Text,StyleSheet,TextInput} from 'react-native'
import { Container, Header, Left, Body, Right, Button, Title } from 'native-base';
import Ripple from 'react-native-material-ripple'
import MCIcon from 'react-native-vector-icons/MaterialCommunityIcons'
import MIcon from 'react-native-vector-icons/MaterialIcons'
import { useNavigation } from '@react-navigation/native';
const My_Header = (props) => {
const {navigate,goBack} = useNavigation();
const openDrawer = () =>{
navigate.openDrawer();
};
const Main_Header =()=>{
return(
<Header style={styles.bg_red} androidStatusBarColor="#ef394e">
<Left style={styles.row}>
<Ripple style={styles.btn} onPress={()=>navigate('Shop_cart')}>
<MCIcon name='cart' style={styles.icon}/>
</Ripple>
<Ripple style={styles.btn} onPress={()=>navigate('Search')}>
<MIcon name='search' style={styles.icon}/>
</Ripple>
</Left>
<Right style={styles.row}>
{
props.head_page_name?
<Text style={styles.texth2}>
{props.head_page_name}
</Text>
:
<Text style={styles.texth1}>
</Text>
}
{
props.right_btn=='back'?
<Ripple style={styles.btn} onPress={()=>goBack(null)} >
<MIcon name='arrow-back' style={[styles.icon,{rotation:180}]} />
</Ripple>
:
<Ripple style={styles.btn} >
<MCIcon name='menu' style={styles.icon} onPress={openDrawer} />
</Ripple>
}
</Right>
</Header>
)
}
source code: https://github.com/P-MBD/shopdroid/tree/Master