Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Navigation: Cannot hide header with nested navigators

I'm using the official react-navigation to handle my navigation. I have one main TabNavigator for the whole app with two tabs (called HitchhikingMapNavigator and SettingsNavigator below), and each tab has a nested StackNavigator:

const HitchhikingMapNavigator = StackNavigator({
  hitchhikingMap: { screen: HitchhikingMapViewContainer },
  spotDetails: { screen: SpotDetailsViewContainer }
}, {
  navigationOptions: {
    header: {
      visible: false
    }
  }
});

const SettingsNavigator = StackNavigator({
  // some other routes
});

export default AppNavigator = TabNavigator({
  hitchhikingMap: { screen: HitchhikingMapNavigator },
  settings: { screen: SettingsNavigator }
}, {
  navigationOptions: {
    header: {
      visible: false,
    },
 },
});

As you can see, I put the headers' visilibility to false everywhere, even in my HitchhikingMapViewContainer's view:

class HitchhikingMapView extends React.Component {

  static navigationOptions = {
    title: 'Map',
    header: {
      visible: false,
    },
    //...other options
  }

And yet, the header bar is still visible:

screenshot

If I don't nest the navigators (i.e. if I put this code, skipping the nested one):

export default AppNavigator = TabNavigator({
  hitchhikingMap: { screen: HitchhikingMapViewContainer },
  settings: { screen: SettingsNavigator }
});

then the header is correctly hidden.

So conclusion: I can't make a header not visible when I have two nested navigators. Any ideas?

like image 513
jeanpaul62 Avatar asked Feb 22 '17 17:02

jeanpaul62


People also ask

How do I hide the header title in react navigation?

How do I hide the header in navigation? To hide the navigation header on Press of a Button setOptions({headerShown: false}); In this example, We will create a stack navigator with a single screen which will have a header and has a button to click.

How do I hide the header in react navigation 5?

In react navigation 5. x you can hide the header for all screens by setting the headerMode prop of the Navigator to false .

How do I navigate to nested screen in react navigation?

If the navigator was already rendered, navigating to another screen will push a new screen in case of stack navigator. In the above case, you're navigating to the Media screen, which is in a navigator nested inside the Sound screen, which is in a navigator nested inside the Settings screen.


3 Answers

For those who are still looking for the answer, I will post it here.

So two solutions:

1st solution: use headerMode: 'none' in the StackNavigator. This will remove the header from ALL screens in the StackNavigator

2nd solution: use headerMode: 'screen' in the StackNavigator and add header: { visible: false } in the navigationOptions of the screens where you want to hide the header.

More info can be found here: https://reactnavigation.org/docs/en/stack-navigator.html

like image 100
jeanpaul62 Avatar answered Oct 09 '22 07:10

jeanpaul62


Starting from v1.0.0-beta.9, use the following,

static navigationOptions = {
    header: null
}
like image 21
h--n Avatar answered Oct 09 '22 06:10

h--n


This worked for me:

headerMode: 'none'
like image 3
Peter Avatar answered Oct 09 '22 06:10

Peter