Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Navigation Show/Hide Drawer Items

Tags:

React Native using React Navigation - Show/Hide Drawer Item

I was wondering if you guys or maybe someone have come up of a better Idea of showing or hiding some menu or Drawer Item under DrawerNavigator.

Basically I have user roles and I want to show/hide selected menu's based on user's role.

My setup now is that I have A DrawerNavigator nested within a StackNavigator.

Menu That Contains My Drawer Items

Hide some drawer items based on user role

Currently Using:

react version 16.0.0-alpha.12

react-native version 0.46.0

react-navigation version 1.0.0-beta.11

like image 493
deejaygeroso Avatar asked Jul 13 '17 09:07

deejaygeroso


People also ask

How do I hide my navigation drawers?

To hide the navigation option from the navigation drawer we will use drawerContent prop of Drawer. Navigator. This prop provides independence to replace default navigation drawer with our custom one. We have added some custom code to customise the navigation drawer.

How do you pass props in a drawer screen?

Here is how you can pass the props to the components from Drawer. Screen easily by replacing the simple component from component={} and replacing it with an anonymous function which will return our component where we need to navigate.


1 Answers

You can create your own custom component that will be rendering drawer items

contentComponent: CustomDrawerContentComponent

inside that component you can define logic on show hide your items

example:

const CustomItems = ({
  navigation: { state, navigate },
  items,
  activeItemKey,
  activeTintColor,
  activeBackgroundColor,
  inactiveTintColor,
  inactiveBackgroundColor,
  getLabel,
  renderIcon,
  onItemPress,
  style,
  labelStyle,
}: Props) => (
  <View style={[styles.container, style]}>
    {items.map((route: NavigationRoute, index: number) => {
      const focused = activeItemKey === route.key;
      const color = focused ? activeTintColor : inactiveTintColor;
      const backgroundColor = focused
        ? activeBackgroundColor
        : inactiveBackgroundColor;
      const scene = { route, index, focused, tintColor: color };
      const icon = renderIcon(scene);
      const label = getLabel(scene);
      return (
        <TouchableOpacity
          key={route.key}
          onPress={() => {
            console.log('pressed')
            onItemPress({ route, focused });
          }}
          delayPressIn={0}
        >
          <View style={[styles.item, { backgroundColor }]}>
            {icon ? (
              <View style={[styles.icon, focused ? null : styles.inactiveIcon]}>
                {icon}
              </View>
            ) : null}
            {typeof label === 'string' ? (
              <Text style={[styles.label, { color }, labelStyle]}>{label}</Text>
            ) : (
              label
            )}
          </View>
        </TouchableOpacity>
      );
    })}
  </View>
);

So in the code above you can define which route will be shown, for instance you can have store with list of items show or hide and from here you can make comparison.

Hope it helps

like image 115
Kirill Avatar answered Sep 30 '22 13:09

Kirill