Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
866 views
in Technique[技术] by (71.8m points)

how to get current routeName in react-navigation-drawer Drawer compoenent?

I have created my App Navigator component with these libraries

  1. react-navigation
  2. react-navigation-stack
  3. react-navigation-drawer and drawer is nested inside main stack.

Drawer stack

const DrawerMenu = createDrawerNavigator(
  {
    Home: {
      screen: Home
    },
    MyAccount: {
      screen: MyAccount
    }
  },
  {
    overlayColor: "rgba(0, 0, 0, 0.7)",
    gestureEnabled: false,
    initialRouteName: "Home",
    contentComponent: Drawer,
    drawerWidth: styles.drawerWidth
  }
);

const DrawerAppContainer = createAppContainer(DrawerMenu);

Main App container or Root

const routes = {
  //independent screens
  Language: {
    screen: Language,
  },
  Welcome: {
    screen: Welcome,
  },
 DetailScreen: {
    screen: DetailScreen,
  },
  Dashboard: {
    screen: DrawerAppContainer,
  },
  Login: {
    screen: Login,
  },
const routeConfig = {
  initialRouteName: "Home",
  headerMode: "none",
  navigationOption: {
    gestureEnabled: false,
  },
};
const AppNavigator = createStackNavigator(routes, routeConfig);

export default createAppContainer(AppNavigator);

In my project, I want the current rendering screen name in the drawer component so I can style route name accordingly(like color, bold fonts).

I tried getting route name with props (this.props.navigation.state.routeName), but its always showing Dashboard.

How do I get nested routeName like Home or MyAccount?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the way I made it work using version 5 of the react navigation drawer component.

In this example I am creating some screens but I want to hide some of them.

First create the navigation container.

 <NavigationContainer>
      {
        <Drawer.Navigator
          initialRouteName='Home'
          drawerContent={(props) => <DrawerContent {...props} />}
        >
          <Drawer.Screen name='Home' component={HomeScreen} />
          <Drawer.Screen
            name='RawMaterial'
            component={RawMaterial}
            options={{ drawerLabel: 'Materia Prima' }}
          />
          <Drawer.Screen
            name='RawMaterialAdd'
            component={RawMaterialAdd}
            options={{ drawerLabel: '' }}
          />
          <Drawer.Screen
            name='RawMaterialEdit'
            component={RawMaterialEdit}
            options={{ drawerLabel: '' }}
          />
        </Drawer.Navigator>
      }
    </NavigationContainer>

Then you will need to created a custom drawer, for the sake of the example is DrawerContent.

import React from 'react';
import { View, StyleSheet } from 'react-native';
import { DrawerContentScrollView, DrawerItem } from '@react-navigation/drawer';

import { Icon } from 'native-base';
export function DrawerContent(props) {
  return (
    <View style={{ flex: 1 }}>
      <DrawerContentScrollView {...props}>
        <View>
          <DrawerItem
            icon={({ color, size }) => <Icon type='AntDesign' name='home' />}
            label='Home'
            focused={getActiveRouteState(
              props.state.routes,
              props.state.index,
              'Home'
            )}
            onPress={() => {
              props.navigation.navigate('Home');
            }}
          />
          <DrawerItem
            icon={({ color, size }) => (
              <Icon type='AntDesign' name='shoppingcart' />
            )}
            focused={getActiveRouteState(
              props.state.routes,
              props.state.index,
              'RawMaterial'
            )}
            label='Materia Prima'
            onPress={() => {
              props.navigation.navigate('RawMaterial');
            }}
          />
        </View>
      </DrawerContentScrollView>
    </View>
  );
}

const getActiveRouteState = function (routes, index, name) {
  return routes[index].name.toLowerCase().indexOf(name.toLowerCase()) >= 0;
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...