编程语言
首页 > 编程语言> > javascript – 如何从createMaterialTopTabNavigatorto其他屏幕导航 – React Navigation?

javascript – 如何从createMaterialTopTabNavigatorto其他屏幕导航 – React Navigation?

作者:互联网

从顶部Tabnavigator导航到其他屏幕时我遇到了一些问题
所以我的应用程序导航是

My Orders Screen from Drawer => Top TabNavigatore (Accepted/Completed) => Order Details

在Route.js中
我把我想要的每一个导航都放在Drawer – Auth导航等等,然后我把StackNavigator包含在这样的Orders屏幕中:

const OrdersStack = createStackNavigator({
  Orders: {
    screen: Orders,
    navigationOptions: ({ navigation }) => ({
      headerLeft: (
        // <TouchableOpacity onPress={() => navigation.dispatch(DrawerActions.toggleDrawer())}>
        <TouchableOpacity
          onPress={() => navigation.dispatch(DrawerActions.toggleDrawer())}
        >
          <Icon
            name="ios-menu"
            size={40}
            style={{ margin: 10 }}
            color="#2F98AE"
          />
        </TouchableOpacity>
      ),
      headerRight: <View />,
      title: "My Orders",
      headerTintColor: "#2F98AE",
      headerStyle: {
        borderBottomColor: "white"
      },
      headerTitleStyle: {
        color: "#2F98AE",
        // textAlign: "center",
        flex: 1,
        elevation: 0,
        fontSize: 25
        // justifyContent: "center"
      }
    })
  }
});

在Orders.js中我把这些:

import React, { Component } from "react";
import { createAppContainer, createStackNavigator } from "react-navigation";
import NavTabs from "../screens/NavTabs";
import NavOrderDetails from "../screens/NavOrderDetails";

// create a component
export default class Orders extends Component {
  render() {
    return <MyOrdersScreen />;
  }
}

export const root = createStackNavigator({
  NavTabs: NavTabs,
  NavOrderDetails: NavOrderDetails
});

const MyOrdersScreen = createAppContainer(root);

正如我在Orders.js中提到的,它包含选项卡和订单详细信息

在Tabs中,我正在创建一个createMaterialTopTabNavigator

import { createMaterialTopTabNavigator } from "react-navigation";
import AcceptedOrders from "../screens/AcceptedOrders";
import CompletedOrders from "../screens/CompletedOrders";

const MainTabs = createMaterialTopTabNavigator(
  {
    Accepted: { screen: AcceptedOrders },
    Completed: { screen: CompletedOrders }
  },
  {
    tabBarOptions: {
      activeTintColor: "#fff",
      inactiveTintColor: "#ddd",
      tabStyle: {
        justifyContent: "center"
      },
      indicatorStyle: {
        backgroundColor: "#fcc11e"
      },
      style: {
        backgroundColor: "#2F98AE"
      }
    }
  }
);
export default MainTabs;

另一个屏幕是OrderDeatils.js

import { createStackNavigator } from "react-navigation";
import OrderDetails from "../screens/OrderDetails";
import React, { Component } from "react";
import { View } from "react-native";
const OrderDetailsStack = createStackNavigator({
  OrderDetails: {
    screen: OrderDetails,
    navigationOptions: () => ({
      title: "Order Details",
      headerRight: <View />,
      headerTintColor: "#2F98AE",
      headerStyle: {
        borderBottomColor: "white"
      },
      headerTitleStyle: {
        color: "#2F98AE",
        flex: 1,
        elevation: 0,
        fontSize: 25
      }
    })
  }
});
export default OrderDetailsStack;

这是一个screenShots它应该解释我的意思

1-我的订单
My Orders

2-订单详情
Order Details

解决方法:

如果我理解,您会担心第一个标题下屏幕顶部显示的空白标题.

那个是由createStackNavigator创建的.

第一个创建名为OrdersStack的第一个Header的Stack.

在内部你有根常量(可能,因为没有完整的代码)正在创建第二个标题.

在root内部,然后用两个屏幕定义你的createMaterialTopTabNavigator,它显示topBar,标签为“accepted”和“completed”.

要隐藏该空白区域,您必须禁用根标题:

export const root = createStackNavigator({
  NavTabs: NavTabs,
  NavOrderDetails: NavOrderDetails
},
   {
     defaultNavigationOptions:{
       header:null
   }
});

UPDATE.

你有两种方法来解决这个问题,但仍然有一个backButton:

1)您可以创建一个父CustomHeader,使用react-navigation的withNavigation HOC,可以了解他的孩子的导航状态.

2)当显示第二个标题时,使用其他方式隐藏父标题.你可以使用this.props.navigation.dangerouslyGetParent().dangerouslyGetParent().setParams({showHeader:false})来完成这个任务.
然后你的OrdersStack将是:

 const OrdersStack = createStackNavigator({
Orders: {
  screen: Orders,
  navigationOptions: ({ navigation }) => {
    var defaultHeader={
        headerLeft: (
        <TouchableOpacity
            onPress={() => navigation.dispatch(DrawerActions.toggleDrawer())}
        >
            <Icon
            name="ios-menu"
            size={40}
            style={{ margin: 10 }}
            color="#2F98AE"
            />
        </TouchableOpacity>
        ),
        headerRight: <View />,
        title: "My Orders",
        headerTintColor: "#2F98AE",
        headerStyle: {
        borderBottomColor: "white"
        },
        headerTitleStyle: {
        color: "#2F98AE",
        // textAlign: "center",
        flex: 1,
        elevation: 0,
        fontSize: 25
        // justifyContent: "center"
        }
    }
    if (navigation.state.params)
        return(navigation.state.params.showHeader?{defaultHeader}:null)
    else return defaultHeader

  }
}


 });

标签:react-navigation,javascript,reactjs,react-native,redux
来源: https://codeday.me/bug/20191003/1846154.html