代码之家  ›  专栏  ›  技术社区  ›  Cactus

使用React-navigation进行React-native:标题未显示在屏幕中

  •  1
  • Cactus  · 技术社区  · 5 年前

    我有一个简单的React原生应用程序,使用React导航进行导航。我的问题是其中一个屏幕的标题和标题没有显示,即使它设置为 navigationOptions :

    import React from "react";
    import { View, Text } from "react-native";
    import {
      createDrawerNavigator,
      createAppContainer,
      createStackNavigator
    } from "react-navigation";
    
    import ArticleList from "./screens/ArticleList";
    import ArticleList2 from "./screens/ArticeList2";
    
    import ArticleWebView from "./screens/ArticleWebView";
    
    // create the Navigator to be used
    const Stack = createStackNavigator({
      ArticleList: { screen: ArticleList },
      ArticleWebView: { screen: ArticleWebView }
    });
    
    const Drawer = createDrawerNavigator(
      {
        Stack: { screen: Stack },
        ArticleList2: { screen: ArticleList2 }
      },
      {
        initialRouteName: "Stack"
      }
    );
    
    // The container for the navigator
    const AppContainer = createAppContainer(Drawer);
    
    class App extends React.Component {
      render() {
        return <AppContainer />;
      }
    }
    
    export default App;
    

    ArticleList2是一个非常简单的组件:

    import React from "react";
    import { View, Text } from "react-native";
    
    import ArticleListComponent from "../components/ArticleListComponent";
    
    class ArticleList2 extends React.Component {
      static navigationOptions = {
      title: 'Link2'
      };
    
      render() {
        return (
          <View>
            <Text>LIst2</Text>
          </View>
        );
      }
    }
    
    export default ArticleList2;
    

    但是,标题和标题不会在应用程序中呈现。有人能帮忙吗?

    1 回复  |  直到 5 年前
        1
  •  7
  •   Zayco    5 年前

    抽屉导航器不包括标题。为了在ArticleList2中创建标题,您必须为组件创建一个新的StackNavigator。

    你必须考虑并规划你的应用程序的导航流程。

    React导航文档的抽屉部分有点不足,但是您可以使用与选项卡部分中描述的相同的原理

    A stack navigator for each tab

    const ArticleList2Stack = createStackNavigator({
      ArticleList2: { screen: ArticleList2 }
    });
    
    const Drawer = createDrawerNavigator(
     {
       Stack: { screen: Stack },
       ArticleList2: { screen: ArticleList2Stack }
     },
     {
       initialRouteName: "Stack"
     }
    );