【问题标题】:Dynamically include header as a component in StackNavigator RootScreen在 StackNavigator RootScreen 中动态包含标题作为组件
【发布时间】:2019-11-21 05:58:17
【问题描述】:

我想在我的导航标签上方有一个自定义标题。我有一个包含在 RootScreen 中的标题组件,但据我所知,我无法动态显示/隐藏它。

const tabBarLabelAndNotifications = (notifications, stack) => {
  return (
    <View style={{display: 'flex', flexDirection: 'row'}}>
      <Text style={{color: '#3F92DF', fontSize: 10, fontWeight: 'bold'}}>{stack}</Text>
      <View style={styles.circle}>
        <Text style={[styles.notifications, styles.fontLatoBold]}>{notifications}</Text>
      </View>
    </View>
  )
}

const EngagementStack = createStackNavigator({
  Engagements: EngagementsScreen,
});

EngagementStack.navigationOptions = {
  tabBarLabel: 'ENGAGEMENTS',
};

const QueryStack = createStackNavigator({
  Queries: QueriesScreen,
});

QueryStack.navigationOptions = {
  tabBarLabel: tabBarLabelAndNotifications(4, 'QUERY')
};

const IssuesStack = createStackNavigator({
  Issues: IssuesScreen,
});

IssuesStack.navigationOptions = {
  tabBarLabel: tabBarLabelAndNotifications(2, 'ISSUES')
};

const TabNavigator = createMaterialTopTabNavigator({
  EngagementStack,
  QueryStack,
  IssuesStack
},
  {
    tabBarOptions: {
      style: {
        backgroundColor: 'transparent'
      },
      labelStyle: {
        color: '#3F92DF',
        fontWeight: 'bold',
        fontSize: 10
      },
      indicatorStyle: {
        height: '100%',
        backgroundColor: 'rgba(82, 170, 231, 0.2)',
        borderRadius: 4
      }
    }
  }
);

const RootStack = createStackNavigator(
  {
    Main: {
      screen: TabNavigator
    },
    EngagementDetailModal: {
      screen: EngagementDetailModal
    },
    RequestForRecordsModal: {
      screen: RequestForRecordsModal
    }
  },
  {
    mode: 'modal',
    headerMode: 'none',
  }
)

const RootNav = createAppContainer(RootStack);

export default function RootScreen() {
  return (
    <View style={{ flex: 1 }}>
      <Header />
      <RootNav />
    </View>
  );
}

从上面的代码中,我想动态显示&lt;Header /&gt; 组件,只要我在页面上而不是模式上。

我尝试在与我发布的代码相同的文件中使用 withNavigation,但检查导航道具只是让我未定义。代码似乎永远不会从根重新渲染。我尝试为每个单独的页面添加 navigationOptions,但它没有呈现我的自定义标题。

编辑 1:根据 documentation。以下代码仅在我的视图中显示选项卡上方的空白区域

static navigationOptions = {
    // headerTitle instead of title
    headerTitle: <Header />,
  };

而导入的组件只是为了测试

export default function Header() {
    return (
        <View><Text>hi</Text></View>
    )
}

【问题讨论】:

    标签: react-native react-navigation


    【解决方案1】:

    试试

    static navigationOptions = ({ navigation, tintColor }) => {
    
        return {
          headerTitle: (
            <Header />
          ),
          headerLeft: (
            <View />
          ),
          headerRight: (
           <View />
          )
        };
      };
    

    【讨论】:

    • 即使这只是在我的标签上方显示一个空白区域。我开始怀疑这是否不是其他原因造成的。无论我尝试什么,我都无法出现任何东西。
    • 你能为这个做点心吗?
    【解决方案2】:

    在您要隐藏/显示模态静态导航选项的屏幕内:

       static navigationOptions = ({ navigation }) => {
        const { state } = navigation;
        let header = <CustomHeader />;
        if (state.params) header = state.params.modalShowing ? null : <CustomHeader />
        return { header }
    }
    

    然后,当您想显示模态时,请执行 setParams:

    this.props.navigation.setParams({modalShowing:!this.state.showModal})
    

    【讨论】:

    • 即使只是尝试在我想要显示自定义标题的页面中使用 navigationOptions,也没有任何显示。根据文档,我应该做类似static navigationOptions = { headerTitle: &lt;CustomHeader /&gt; } 之类的操作,它仍然不显示标题,它只显示一个空的白色块
    • 你试过我的解决方案了吗?这与我用来隐藏/显示标题的方法相同,并且运行顺畅
    猜你喜欢
    • 2012-11-22
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 2019-11-23
    • 1970-01-01
    • 2014-07-29
    相关资源
    最近更新 更多