简体   繁体   English

在 React Native 中从底部选项卡导航器的标题导航

[英]Navigation from the header of the bottom tab navigator in react native

I am using bottom tab navigator inside stack navigator of react-navigation in react native.我在 react native 的 react-navigation 的堆栈导航器中使用底部选项卡导航器。 I want to navigate from the left or right element of the header when the stack comes to the bottom tab navigator.当堆栈来到底部选项卡导航器时,我想从标题的左侧或右侧元素进行导航。 I added the left and right button but when I am trying to navigate it gives me error my navigators are as follow.我添加了左右按钮,但是当我尝试导航时,它给了我错误,我的导航器如下所示。

import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import FIcon from 'react-native-vector-icons/FontAwesome5';
import { TouchableOpacity, View } from 'react-native';
import AIcon from 'react-native-vector-icons/AntDesign';




import React from 'react';
import Login from './src/component/Login/Login';
import Registration from './src/component/Registration/Registration';
import Navigator from './src/component/AfterLogingIn/AfterLoginRoute';
import ChatDashboard from './src/component/AfterLogingIn/Chat/Chatboard';

const navigator = createStackNavigator({
  login: {
    screen: Login,
    navigationOptions: {
      header: null,
    },
  },
  registration: {
    screen: Registration,
    navigationOptions: {
      header: null,
    },
  },
  Navigator: {
    screen: Navigator,
    navigationOptions: {
      headerStyle: {
        backgroundColor: '#192a56'
      },

      headerRight: (
        <View style = {{flexDirection: 'row'}}>
          <TouchableOpacity style={{ marginRight: 20 }}
          >
            <FIcon name='search' style={{ color: 'grey' }} size={25} />
          </TouchableOpacity>
          <TouchableOpacity style={{ marginRight: 10 }}
            onPress={() => this.navigation.navigate('ChatDashboard')}
          >
            <AIcon name='message1' style={{ color: 'grey' }} size={25} />
          </TouchableOpacity>
        </View>
      ),
    }
  },
  SignOut: {
    screen: Profile,
  },
  ChatDashboard: {
    screen: ChatDashboard,
  },
},

  {
    initialRouteName: 'login',
  }
);

// Export it as the root component
export default createAppContainer(navigator);

Here is the bottom tab navigator这是底部的标签导航器

const TabNavigator = createBottomTabNavigator({
  posts:{
    screen: Posts,
    navigationOptions: {
      title: 'ABC',
      tabBarLabel: 'Posts',
      tabBarIcon: ({tintColor}) =>(
        <MCIcon name='calendar-text' color = {tintColor} size = {25} />
      )
    }
  },
  events: {
    screen: Events,
    navigationOptions:{
      tabBarLabel: 'Events',
      tabBarIcon: ({tintColor}) =>(
        <MIcon name = 'event' color = {tintColor} size = {25} />
      )
    }
  },
  addPost:{
    screen: AddPost,
    navigationOptions:{
      tabBarLabel: 'Add Post',
      tabBarIcon: ({tintColor}) =>(
        <FIcon name="plus-square" color={tintColor} size = {25} />
      )
    }
  },
  alumniStory: {
    screen: AlumniStory,
    navigationOptions:{
      tabBarLabel: 'Alumni Story',
      tabBarIcon: ({tintColor}) =>(
        <FIcon name = 'book-reader' color = {tintColor} size = {25} />
      )
    }
  },
  Profile: {
    screen: Profile,
    navigationOptions: {
      tabBarLabel: 'Profile',
      tabBarIcon: ({tintColor}) =>(
        <FIcon name = 'user' color= {tintColor} size = {25}  />
      )
    }
  },
},

{
  swipeEnabled: true,
  tabBarOptions: {
    style: {
      backgroundColor: '#192a56',
    }
  },
}

);

How do I navigate from header?如何从标题导航?

Because you try using navigation in renderHeader and don't have navigation in register screen file, so let try this instead of defined in that file, we defined it in the Navigator component.因为您尝试在renderHeader使用navigation并且在注册屏幕文件中没有navigation ,所以让我们试试这个而不是在那个文件中定义,我们在 Navigator 组件中定义它。

class Navigator extends React.Component {
      static navigationOptions = ({ navigation, navigationOptions }) => {
        return {
          headerStyle: {
            backgroundColor: '#192a56'
          },
          headerRight: (
            <View style = {{flexDirection: 'row'}}>
              <TouchableOpacity style={{ marginRight: 20 }}
              >
                <FIcon name='search' style={{ color: 'grey' }} size={25} />
              </TouchableOpacity>
              <TouchableOpacity style={{ marginRight: 10 }}
                onPress={() => navigation.navigate('ChatDashboard')}
              >
                <AIcon name='message1' style={{ color: 'grey' }} size={25} />
              </TouchableOpacity>
            </View>
          ),
        };
      };
    }

Offical docs here官方文档在这里

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

相关问题 在 React native 中创建自定义底部选项卡导航器 - Create custom bottom tab navigator in React native 从选项卡导航器选项卡按下触发类函数调用-反应本机/导航 - Trigger Class Function Call from Tab Navigator Tab Press - React Native/Navigation 如何在底部选项卡导航器 react-navigation 中卸载非活动屏幕? - How to unmount inactive screens in bottom tab navigator react-navigation? React Navigation V5 在 Stack Navigator 中隐藏底部标签栏 - React Navigation V5 hide Bottom tab bar in Stack Navigator 刷新控件不适用于本机反应中的底部选项卡导航器 - Refresh Control not working with bottom tab navigator in react native 从抽屉导航器到标签导航器反应本机传递参数 - React Native Pass Param From Drawer Navigator to Tab Navigator 如何在反应导航 5.x 中从选项卡导航(底部选项卡)中隐藏 header? - How to hide the header from Tab navigation (bottom-tabs) in react navigation 5.x? React Native:尝试将 Stack Navigation 传递到 Bottom tab Navigation - React Native: trying to Pass Stack Navigation into Bottom tab Navigation Header 标题不随底部导航而改变 React Native Navigation v5 - Header title not changing with bottom navigation React Native Navigation v5 带有选项卡导航器的堆栈导航器(React Native) - Stack Navigator with Tab Navigator (React Native)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM