首页 > 解决方案 > 如何有条件地更改同一导航器中不同反应本机屏幕的 screenOptions?

问题描述

我有一个带 2 个屏幕的导航器。在其中一个屏幕中,我想显示headerRight组件,而在另一个屏幕中,我不想显示headerRight组件。

我的导航如下所示:

    <NavigationContainer>
      <Stack.Navigator screenOptions={{ 
        headerShown: true, 
        headerRight: () => <Switch /> // conitionally change this
      }}>
        <Stack.Screen name="Vocabulary" component={bottomNavigator} />
        <Stack.Screen name="Definition" component={Definition}  />
      </Stack.Navigator>
    </NavigationContainer>

标签: javascriptreactjsreact-nativeexporeact-navigation

解决方案


试试这个方法

componentDidMount() {
    this.props.navigation.setOptions({
      headerRight: <Switch />
    });
}

推荐阅读