0

I have a home screen built with React-Navigation 3.x. I have a header, some navigation icons, and a bottom tab menu. It is working well, but now I want to add a drawer menu to the screen and add an icon at the top right corner to toggle the drawer.

Here's a simplified version of my home screen (App.js):

import { createStackNavigator, createAppContainer, createBottomTabNavigator } from 'react-navigation';
import Activity1 from './Activity/Activity1';
import Activity2 from './Activity/Activity2';
import Activity3 from './Activity/Activity3';
import Calendar from './Screens/Calendar';
import Graph from './Screens/Graph';
import DrawerMenu from './components/DrawerMenu';

class HomeScreen extends React.Component {
   static navigationOptions = {
      title: 'Home',
      headerRight: (<Button onPress={() => this.props.navigation.toggleDrawer()} title='Menu' />)
    };

    render() {
       return (
          <View style={styles.container}>
             <View style={styles.iconContainer}>
                <Icon name='icon1' onPress={this.navToActivity1} />
                <Icon name='icon2' onPress={this.navToActivity2} />
                <Icon name='icon3' onPress={this.navToActivity3} />
             </View>
             <View>
                <DrawerMenu />
             </View>
          </View>
       );
    }
}

//create my main navigation stacks here 
const Home = createStackNavigator({
   HomeScreen,
   Activity1,
   Activity2,
   Activity3,
});

//The following two are for the bottom tab bar only
const Calendar = createStackNavigator({ Calendar });
const Graph = createStackNavigator({ Graph });

const BottomTabNav = createBottomTabNavigator({
    Home, Calendar, Graph
});

export default createAppContainer(TabNavigator);

And here's the code for DrawerMenu.js

import { Dimensions } from 'react-native';
import { createDrawerNavigator, createAppContainer } from 'react-navigation';
import Settings from './Settings';
import Profile from './Profile';

const SCREENWIDTH = Dimensions.get('window').width;

const DrawerConfig = {
  drawerWidth: SCREENWIDTH * 0.5,
  drawerPosition: 'right',
};

const DrawerMenu = createDrawerNavigator({
       Settings: { screen: Settings },
       Profile: { screen: Profile },
   },
   DrawerConfig
);

export default createAppContainer(DrawerMenu);

I couldn't get the drawer to work. When I click on the "Menu" button at the top right corner in the Home Screen to invoke toggleDrawer(), I got an "undefined is not an object (evaluating 'ae.props.navigation')" error.

The drawer cannot be activated using gesture either, so I think I am not adding it correctly. What did I do wrong here? Thanks!!

Mochi08
  • 167
  • 3
  • 15
  • did you install https://www.npmjs.com/package/react-native-gesture-handler check installation here https://reactnavigation.org/docs/en/getting-started.html#installation – Vinil Prabhu Apr 04 '19 at 09:45
  • I verified that gesture-handler was already installed and linked. I also followed your link and added the import statements and override method to Android MainActivity.java. Unfortunately it did not make any difference. – Mochi08 Apr 04 '19 at 18:23

1 Answers1

-1

install the package the react-native-drawer https://www.npmjs.com/package/react-native-drawer

<Drawer
    type="overlay"
    ref={ref => (this._drawer = ref)}
    content={<Sidebar />}
    tapToClose={true}
    openDrawerOffset={0.2} // 20% gap on the right side of drawer
    panCloseMask={0.2}
    closedDrawerOffset={-3}
    styles={drawerStyles}
    tweenHandler={ratio => ({ main: { opacity: (2 - ratio) / 2 } })}
  >

cds