Search code examples
react-nativereact-navigationreact-navigation-stackreact-navigation-drawer

Use drawer navigation without having a Screen inside it


I have a following stack navigator

<Stack.Navigator>
  <Stack.Screen name="Home" component={HomeScreen} />
  <Stack.Screen name="Settings" component={SettingsScreen} />
  <Stack.Screen name="AboutUs" component={AboutUsScreen} />
  <Stack.Screen name="ContactUs" component={ContactUsScreen} />
  <Stack.Screen name="Blog" component={BlogScreen} />
</Stack.Navigator>

I want to have drawer (with custom drawer content) without using nesting drawer navigators.


Solution

  • I saw your issue and I am trying to helping you. I have made a custom design for drawer components. -firstly you can create an extra file for drawer Design like DrawerComponent.js and import in your code where you are creating a drawer navigator

    import DrawerComponent from "./DrawerComponent";
    const Primary_Nav = createDrawerNavigator(
    {
      screen1: {
      screen: screen1,
      navigationOptions: {
      drawerLabel: () => null
       }
     },
    screen2: {
      screen: screen2,
      navigationOptions: {
        drawerLabel: "detail"
      }
    },
     {
     initialRouteName: "screen1",
    drawerPosition: "left",
    drawerType: "slide",
     contentComponent: DrawerComponent //<<< i added this
    }
    );
    
    const PrimaryNav = createAppContainer(Primary_Nav);
    export default PrimaryNav;
    

    now in the DrawerComponent.js you can make custom component contents what you want.

    import React, { Component } from "react";
    import { Text, View, TouchableOpacity } from "react- 
    native";
    
    export default class DrawerComponent extends Component {
      constructor(props) {
        super(props);
    
        this.state = {
    
        };
      }
    
      render() {
        const { navigation } = this.props;
        return (
          <View style={{ flex: 1, paddingVertical: 40,paddingHorizontal: 20 }}>
            <TouchableOpacity
              style={{ margin: 20 }}
              onPress={() => navigation.navigate("screen1")}
            >
              <Text>Home</Text>
            </TouchableOpacity>
            <TouchableOpacity
              style={{ margin: 20 }}
              onPress={() => navigation.navigate("screen2")}
            >
              <Text>Detail</Text>
            </TouchableOpacity>
      </View>
      );
     }
    }
    

    I hope it will helpful for you. thank you and happy coding.