简体   繁体   English

React Native Navigation v5 从底部选项卡触发抽屉

[英]React Native Navigation v5 triggering a drawer from bottom tabs

Currently I am trying to tigger the drawer from the bottom nav as you can see here:目前,我正在尝试从底部导航中拨动抽屉,如下所示:

在此处输入图片说明

My current tabs is the following:我当前的标签如下:

 <NavigationContainer>
  <Tab.Navigator
    screenOptions={({ route }) => ({
      tabBarIcon: ({ focused }) => {
        let iconName;

        switch (route.name) {
          case 'Home':
            iconName = 'window-maximize';
            break;
          case 'Purchases':
            iconName = 'tags';
            break;
          case 'Notifications':
            iconName = 'bell';
            break;
          case 'Checkout':
            iconName = 'shopping-cart';
            break;
          case 'Orders':
            iconName = 'dollar';
            break;
          case 'Menu':
            iconName = 'navicon';
            break;
        }

        return (
          <Icon
            name={iconName}
            size={20}
            color={focused ? 'blue' : 'grey'}
          />
        );
      },
    })}
    tabBarOptions={{
      showLabel: false,
    }}>
    <Tab.Screen name="Home" component={LandingScreens} />
    <Tab.Screen name="Purchases" component={PurchasesScreens} />
    <Tab.Screen name="Notifications" component={Notifications} />
    <Tab.Screen name="Checkout" component={CheckoutScreens} />
    <Tab.Screen name="Orders" component={OrdersScreens} />
    <Tab.Screen
      name="Menu"
      component={Drawer}
      listeners={({ navigation }) => ({
        tabPress: (e) => {
          e.preventDefault();
        },
      })}
    />
  </Tab.Navigator>
</NavigationContainer>

And my Drawer Component is just going to be a custom Drawer component:我的 Drawer 组件只是一个自定义的 Drawer 组件:

const Drawer = ({ navigation }) => {
    const Drawer = createDrawerNavigator();

    return (
      <Drawer.Navigator
        drawerContent={(props) => <CustomDrawerContent {...props} />}>
        <Drawer.Screen name="Home" component={LandingScreens} />
      </Drawer.Navigator>
    );
  };

My question is something like this possible?我的问题是这样的可能吗? I was trying to tigger the navigation.openDrawer() from the tabPress but couldn't find the method.我试图跳跳虎从该navigation.openDrawer() tabPress但找不到方法。 I am doing this right, or would I need some sort of custom bottomNav?我这样做是对的,还是需要某种自定义的底部导航? Any help is much appreciated!任何帮助深表感谢!

Thanks.谢谢。

You can either use DrawerActions.openDrawer :您可以使用DrawerActions.openDrawer

import { DrawerActions } from '@react-navigation/native'

    <Tab.Screen
      name="Menu"
      component={Drawer}
      listeners={({ navigation }) => ({
        tabPress: (e) => {
          e.preventDefault();

          navigation.dispatch(DrawerActions.openDrawer())
        },
      })}
    />

A better solution is to nest the tabs inside the drawer so navigation.openDrawer() works.更好的解决方案是将选项卡嵌套在抽屉内,以便navigation.openDrawer()工作。

https://reactnavigation.org/docs/nesting-navigators#navigator-specific-methods-are-available-in-the-navigators-nested-inside https://reactnavigation.org/docs/nesting-navigators#navigator-specific-methods-are-available-in-the-navigators-nested-inside

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM