简体   繁体   English

React Navigation 6.0 双标头问题

[英]React Navigation 6.0 double header issue

I have same code structure of navigation of React Navigation 5.x我有相同的 React Navigation 5.x 导航代码结构

    ...
    "@react-navigation/bottom-tabs": "^5.11.11",
    "@react-navigation/native": "^5.9.4",
    "@react-navigation/stack": "^5.14.5",
    ...

i upgraded React Navigation 5 to 6.x我将 React Navigation 5 升级到 6.x

 "@react-navigation/bottom-tabs": "^6.0.5",
    "@react-navigation/native": "^6.0.2",
    "@react-navigation/stack": "^6.0.7",

When i run the code i got double header, above one header name is Tabs Screen name and below one is headerTitle name当我运行代码时,我得到了双标题,上面一个标题名称是选项卡屏幕名称,下面一个是标题标题名称

These is the code structure of my navigation这些是我导航的代码结构

const HomeStack = createStackNavigator();
const HomeStackScreen = ({navigation, props}) => (
  <HomeStack.Navigator>
    <HomeStack.Screen
      name="HomeMain"
      component={HomeMain}
      options={{
        headerTitle: 'Delivery App',
      }}
    />
    <HomeStack.Screen
      name="Search"
      component={Search}
      options={{
        headerTitle: 'Search',
        headerTitleStyle: {
          fontFamily: 'Muli-Regular',
        },
      }}
    />

  ...

  </HomeStack.Navigator>
);

const CartStack = createStackNavigator();
const CartStackScreen = () => (
  <CartStack.Navigator>
    <CartStack.Screen
      name="CART"
      component={Cart}
      options={({route}) => ({
        headerTitleStyle: {
          fontFamily: 'Muli-Regular',
        },
      })}
    />
 ...
  </CartStack.Navigator>
);

const ProfileStack = createStackNavigator();
const ProfileStackScreen = () => (
  <ProfileStack.Navigator>
    <ProfileStack.Screen
      name="ProfileMain"
      component={ProfileMain}
      options={({route}) => ({
        headerTitle: 'Profile' /*headerShown: false*/,
        headerTitleStyle: {
          fontFamily: 'Muli-Regular',
        },
      })}
    />
    ...
  </ProfileStack.Navigator>
);

const AppTabs = createBottomTabNavigator();
const AppTabsScreen = props => {
  return (
    <AppTabs.Navigator
      tabBarOptions={{
        activeTintColor: '#00D084',
        inactiveTintColor: '#C6CDD7',
      }}>
      <AppTabs.Screen
        name="Home"   //<------ This  name is showing conflict is here
        component={HomeStackScreen}
        options={{
          tabBarIcon: props => (
            <Icon.Ionicons name="home" size={props.size} color={props.color} />
          ),
        }}
      />
      <AppTabs.Screen
        name="Cart"
        component={CartStackScreen}
        options={{
          tabBarIcon: props => (
            <Icon.Ionicons name="cart" size={props.size} color={props.color} />
          ),
          tabBarBadge: props.cartCount,
        }}
      />
      <AppTabs.Screen
        name="Account"
        component={ProfileStackScreen}
        options={{
          tabBarIcon: props => (
            <Icon.Ionicons
              name="person"
              size={props.size}
              color={props.color}
            />
          ),
        }}
      />
    </AppTabs.Navigator>
  );
};

Where do we change to fix this issue, i have tried headerShow null or false also but it hide only 2nd Header.我们在哪里更改以解决此问题,我也尝试过 headerShow null 或 false,但它仅隐藏了第二个标题。 I want to hide the 1st one.我想隐藏第一个。

Here is the screenshot这是屏幕截图

在此处输入图片说明

You need to add headerShown: false to the Tab Navigator.您需要将headerShown: false添加到 Tab Navigator。

eg例如

<AppTabs.Navigator
      screenOptions: {{ headerShown: false }}
      tabBarOptions={{
        activeTintColor: '#00D084',
        inactiveTintColor: '#C6CDD7',
      }}>
{...code}

</AppTabs.Navigator/>

That is if you want to remove the header added by the Tab Navigation.也就是说,如果您想删除选项卡导航添加的标题。 You can do the same for Stack navigators if you want to remove that one.如果您想删除堆栈导航器,您可以对堆栈导航器执行相同操作。

If you don't want to remove header from all of the tab navigators, you can individually add it like this:如果您不想从所有选项卡导航器中删除标题,您可以像这样单独添加它:

     <AppTabs.Screen
        name="Account"
        component={ProfileStackScreen}
        options={{
          headerShown: false
          // other options
        }}
      />

And that will remove header from only that tab.这将仅从该选项卡中删除标题。

Ref: https://reactnavigation.org/docs/headers参考: https : //reactnavigation.org/docs/headers

I have resolved my issue by this line of code.我已经通过这行代码解决了我的问题。

<Tab.Navigator initialRouteName="Home" screenOptions={{tabBarActiveTintColor:Colors.appPurple,headerShown:false}}> {...code} </Tab.Navigator> <Tab.Navigator initialRouteName="Home" screenOptions={{tabBarActiveTintColor:Colors.appPurple,headerShown:false}}> {...code} </Tab.Navigator>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM