Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add sidebar drawer with react native navigation v2?

With react-native-navigation v1 you can set up a drawer like this:

drawer: {
    left: {
        screen: 'ScreenName'
    }
}

In docs of react-native-navigation they mention that drawer is still supported, drawer support

but there in no example of its usage. I tried with same way as in v1, but it didn't work. Is there anyone who has achieved it somehow?

like image 698
angelos_lex Avatar asked Jul 26 '18 06:07

angelos_lex


People also ask

How do I add a navigation drawer in react native?

Create two separate classes "DashboardScreen" and "WelcomeScreen" in the react native app to display on screen. Add these screens to createStackNavigator and add "md-menu" icon of 'react-native-vector-icons/Ionicons' package. On pressing the menu icon, call navigation. openDrawer() method to open drawer.


1 Answers

In RNN V2 and up you can add Drawer with simply using sideMenu instead of old drawer option Ex :

Navigation.events().registerAppLaunchedListener(() => {
  Navigation.setRoot({
    root: {
      sideMenu: {
        id: "sideMenu",
        left: {
          component: {
            id: "Drawer",
            name: "navigation.Drawer"
          }
        },
        center: {
          stack: {
            id: "AppRoot",
            children: [{
              component: {
                id: "App",
                name: "navigation.AppScreen"
              }
            }]
          }
        }
      }
    }
  });
}

Take a look at this and navigate to sideMenu

and in order to close the drawer use Navigation.mergeOptions and pass visible false like this

<Button onPress={this.hideSideMenu}/>

hideSideMenu() {
  Navigation.mergeOptions(this.props.componentId, {
    sideMenu: {
      left: {
        visible: false
      }
    }
  });
}
like image 187
devoka Avatar answered Nov 09 '22 04:11

devoka