【问题标题】:React-navigation How to put StackNavigator in TabRouter screenReact-navigation 如何将 StackNavigator 放入 TabRouter 屏幕
【发布时间】:2017-05-23 20:31:26
【问题描述】:

我遇到了react-navigation 的一些问题。我想要一个自定义标签栏,其中一个屏幕由StackNavigator 组成。

所以基本上,我有

Tab
  -- StackNavigator
     -- Dashboard (main)
     -- Wallet
     -- Etc...
  -- Other tabs...

为了制作我的自定义标签,我使用了TabRouter、createNavigationContainer 和 createNavigator

const Tabs = TabRouter(
  {
    Main: {
      screen: Main,
    },
  },
  {
    initialRouteName: 'Main',
    tabBarComponent: TabView.TabBarBottom,
    swipeEnabled: false,
    animationEnabled: false,
    backBehavior: 'none',
  },
);

const CustomTabView = ({ router, navigation }) => {
  const { routes, index } = navigation.state;
  const ActiveScreen = router.getComponentForState(navigation.state);
  return (
    <View style={{ flex: 1 }}>
      <ActiveScreen
        navigation={addNavigationHelpers({
          ...navigation,
          state: routes[index],
        })}
      />
      <View style={styles.tabContainer}>
        {routes.map(route => (
          <TouchableOpacity                          // custom tab items
            key={route.routeName}
            onPress={() => navigation.navigate(route.routeName)}
          >
            <TabItem />

          </TouchableOpacity>
        ))}
      </View>
    </View>
  );
};

const TabBar = createNavigationContainer(createNavigator(Tabs)(CustomTabView));

主要在哪里:

const Main = StackNavigator({
  Dashboard: Dashboard,
  Wallet: Wallet,
  ...
}, { initialRouteName: 'Dashboard' });

但这不是因为 Dashboard 必须声明一个屏幕。

我做错了什么?我不知道。我们如何在 TabBar 屏幕中放置 StackNavigator?

【问题讨论】:

    标签: react-native react-navigation


    【解决方案1】:

    幸运的是,这很容易。

    StackNavigator(或任何其他导航器)返回的是 React 组件。这意味着您可以将它用作任何其他路由的screen 组件。

    因此,您的一条路线screen 将是StackNavigator。

    示例代码

    const Tabs = TabNavigator(
      {
        Main: {
          screen: StackNavigator({
            Dashboard: {
              screen: Dashboard,
            }
            Wallet: {
              screen: Wallet,
            }
          }),
        },
      },
      {
        initialRouteName: 'Main',
        tabBarComponent: TabView.TabBarBottom,
        swipeEnabled: false,
        animationEnabled: false,
        backBehavior: 'none',
      },
    );
    

    【讨论】:

    • 它有一段时间没有工作,突然间,它工作了。谢谢!但是,堆栈导航器没有 NavBar (header) 你知道我的代码有什么问题吗? ActiveScreen 不应该包含导航栏吗?还是我必须在CustomTabView中的ActiveScreen之前手动添加一个?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多