【问题标题】:How to Navigate from DRAWER NAVIGATION to BOTTOM TAB BAR NAVIGATION in react native?如何在本机反应中从 DRAWER NAVIGATION 导航到 BOTTOM TAB BAR NAVIGATION?
【发布时间】:2021-01-25 19:29:26
【问题描述】:

此网址:https://streamable.com/no6anz,是展示我的应用导航的视频,我希望能够在我的标签中从抽屉屏幕导航到 SPECIFIC 标签屏幕导航。

例如:如果我按下“消息”抽屉项目,我想导航到 我的帐户 选项卡 屏幕标签导航器中。

到目前为止,我的实现并不是最好的:

import React, {Fragment, Component} from 'react';
import {View, StyleSheet} from 'react-native';
import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';
import {createDrawerNavigator} from '@react-navigation/drawer';
import {createStackNavigator} from '@react-navigation/stack';

import Home from '../Views/HomeView';
import Cart from '../Views/CartView';
import MyAccount from '../Views/MyAccountView';
import Sidebar from '../Views/Sidebar';

import HomeIconColor from './CustomSVGIcons/HomeIconColor';
import CartIconColor from './CustomSVGIcons/CartIconColor';
import PersonIconColor from './CustomSVGIcons/PersonIconColor';
import MenuIconColor from './CustomSVGIcons/MenuIconColor';

import Contact from '../Views/screens/Contact';
import Messages from '../Views/screens/Messages';
import {
  NavigationContainer,
  DrawerActions,
  useNavigation,
} from '@react-navigation/native';
import {BottomTabBar} from '@react-navigation/bottom-tabs';
import Login from '../Views/screens/Login';
import {create} from 'react-test-renderer';
import {createSwitchNavigator} from '@react-navigation/compat';

const Tab = createBottomTabNavigator();
const Drawer = createDrawerNavigator();
const Stack = createStackNavigator();
const Switch = createSwitchNavigator();

class Navigator extends Component {
  constructor() {
    super();
    this.state = {};
  }
  render() {
    return (
      <NavigationContainer>
        <MyDrawerNavigation />
      </NavigationContainer>
    );
  }
}
const MyTabs = (props) => {
  const navigation = useNavigation();
  const Tab = createBottomTabNavigator();
  return (
    <Tab.Navigator
      screenOptions={({route}) => ({
        tabBarButton: ['Contact', 'Route2ToExclude'].includes(route.name)
          ? () => {
              return null;
            }
          : undefined,
      })}>
      <Tab.Screen
        name="Home"
        component={Home}
      />
      <Tab.Screen
        name="MyAccount"
        component={MyAccount}
      />
      <Tab.Screen
        name="Cart"
        component={Cart}
      />
      <Tab.Screen
        name="Sidebar"
        component={Sidebar}
      />
      <Tab.Screen name="Contact" component={Contact} />
    </Tab.Navigator>
  );
};
const MyDrawerNavigation = () => {
  return (
    <Drawer.Navigator drawerPosition={'right'}>
      <Drawer.Screen
        name="Home"
        component={MyTabs}
        initialParams={{screen: 'Home'}}
      />
      <Drawer.Screen
        name="My Account"
        component={MyTabs}
        initialParams={{screen: 'MyAccount'}}
      />
    </Drawer.Navigator>
  );
};    
const styles = StyleSheet.create({
      tabNav: {
        backgroundColor: '#000000',
      },
    });

export default Navigator;

【问题讨论】:

    标签: reactjs react-native


    【解决方案1】:

    已解决!:

    经过广泛深入的研究;我使用 onPress 处理程序创建了自己的自定义抽屉。

    onPress 处理程序启动导航到指定屏幕的操作。

    注意:您必须将导航引用传递给顶级导航容器。您还必须将相同的引用传递给您的自定义抽屉模块(或该关注的任何模块)

    这是我的自定义抽屉组件:

    import React, {Component} from 'react';
    import {Text, View, Button, StyleSheet} from 'react-native';
    
    import {useNavigation, CommonActions} from '@react-navigation/native';
    import * as rootNavigator from '../components/rootNavigator';
    
    const CustomDrawer = () => {
      return (
        <View style={styles.container}>
          <Button
            title="My Account"
            onPress={() => rootNavigator.navigate('MyAccount')}
          />
          <Button
            title="Contact"
            onPress={() => rootNavigator.navigate('Contact')}
          />
          <Button title="Cart" onPress={() => rootNavigator.navigate('Cart')} />
        </View>
      );
    };
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        padding: 30,
      },
    });
    export default CustomDrawer;
    

    这是我返回 DrawerNavBar 的代码:

    const MyDrawerNavigation = () => {
      return (
        <NavigationContainer ref={navigationRef}>
          <Drawer.Navigator
            drawerPosition={'right'}
            drawerContent={(props) => <CustomDrawer {...props} />}>
            <Drawer.Screen name="Tabs" component={MyTabs} />
            <Drawer.Screen name="Contact" component={Contact} />
          </Drawer.Navigator>
        </NavigationContainer>
      );
    };
    

    【讨论】:

      【解决方案2】:

      您可以将屏幕作为初始参数传递,它会重定向到正确的选项卡。

        <Drawer.Screen name="MyTabs" component={'Tab Component Name'} initialParams={{screen:'MyAccount'}}/>
      

      【讨论】:

      • 您好,感谢您的快速回复!它已经工作了(使用反应导航 v5),谢谢!!! :D
      • 嘿Guruparan 实际上,它不起作用...应用程序行为怪异,有时无法导航到特定选项卡...
      • 是的,我已经更新了问题;检查更新后的代码 MyDrawerNavigation =()=> 请指教,谢谢!
      • 这是一个链接,展示了应用程序在 1 分 30 秒时的异常行为streamable.com/p4vg18
      • 我正在检查这个
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-26
      • 2018-11-07
      • 1970-01-01
      • 2020-06-27
      • 1970-01-01
      相关资源
      最近更新 更多