Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User role wise drawer navigation in react-native

I have added react-navigation-drawer for implementing drawer navigation in my app. I have created a file named PrimaryNav.js and added all navigation code in it.

import Login from './components/Login';
import Employee from './pages/Employee';
import { createAppContainer,SafeAreaView, } from 'react-navigation'
import { createDrawerNavigator, DrawerItems } from 'react-navigation-drawer';
import React from 'react';

const Primary_Nav = createDrawerNavigator({
    Login: {
      screen: Login,
      navigationOptions: {
        drawerLabel: () => null
      }
    },
    Home_kitchen: {
      screen: Home_kitchen,
      navigationOptions: {
        drawerLabel: "Home"
      }
    },
    Employee: {
      screen: Employee,
      navigationOptions:{
        drawerLabel:"Employee",
      }
    },
  },{
    initialRouteName:'Login',
    drawerPosition: 'left',
    drawerType: "slide",
    }
});

const PrimaryNav = createAppContainer(Primary_Nav);
export default PrimaryNav;

Something like above. I have called this file in the App.js, the issue I am facing is I need to set a drawer item based on the role which the user has. So if the user role is cashier he should not be able to see all the menu.

All the pages are coming properly in the drawer menu but the question is how should I want to manage menu role wise in my app and changed the menu based on the roles of the user?

like image 594
Maq Avatar asked Jan 21 '26 09:01

Maq


1 Answers

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

import DrawerComponent from "./DrawerComponent";

const Primary_Nav = createDrawerNavigator(
{
 Login: {
  screen: Login,
  navigationOptions: {
    drawerLabel: () => null
  }
},
Home_kitchen: {
  screen: Home_kitchen,
  navigationOptions: {
    drawerLabel: "Home"
  }
},
Employee: {
  screen: Employee,
  navigationOptions: {
    drawerLabel: "Employee"
  }
}
},
{
  initialRouteName: "Login",
  drawerPosition: "left",
  drawerType: "slide",
  contentComponent: DrawerComponent // i added this DrawerComponent
}
);

const PrimaryNav = createAppContainer(Primary_Nav);
export default PrimaryNav;

now in the DrawerComponent.js

import React, { Component } from "react";
import { Text, View, TouchableOpacity } from "react-native";

export default class DrawerComponent extends Component {
  constructor(props) {
    super(props);

    this.state = {
      role: 1 // i used 1 for cashier and 0 for chef
    };
  }

  render() {
    const { role } = this.state;
    const { navigation } = this.props;
    return (
      <View style={{ flex: 1, paddingVertical: 40, paddingHorizontal: 20 }}>
        <TouchableOpacity
          style={{ margin: 20 }}
          onPress={() => navigation.navigate("Home_kitchen")}
        >
          <Text>Home</Text>
        </TouchableOpacity>
        {role ? (
          <TouchableOpacity
            style={{ margin: 20 }}
            onPress={() => navigation.navigate("Employee")}
          >
            <Text>Employee</Text>
          </TouchableOpacity>
        ) : null}
      </View>
    );
  }
}

if you are change the role to 0 then the Employee tab is disable in Drawer Navigator I have user the ternary operator for conditions. you can modify is as you can want. hope it will helpful for you.

like image 179
shammi Avatar answered Jan 22 '26 22:01

shammi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!