Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to display headers in react navigation with TabNavigation

I noticed that views in StackNavigation show the header title but if I set those same screens in a TabNavigation it doesn't show a header. It only shows a header if I wrap a StackNavigation either around each tab, or wrap the TabNavigation nested inside a StackNavigation.

Why don't screens in TabNavigation show a header - is that expected behavior? If so, is it better to have a StackNavigation in each tab, or one big StackNavigation around the TabNavigation?

// tabs navigation doesn't show a header title in each screen

const TabsNavigator = TabNavigator({   Home: {     screen:HomeScreen,   },   Profile: {     screen: ProfileScreen,   }, }, {   tabBarOptions: {     activeTintColor: '#e91e63',   },   navigationOptions: {     header: {       visible: true,     },   }, }); 

Header shows when I wrap it in a StackNavigator

default StackNavigator({   Home: { screen: TabsNavigator }, }); 

Or is it better to do it this way

 export TabsNavigator = TabNavigator({       Home: {         screen:StackNavigator({           Home: { screen: HomeScreen },         }),       },       Profile: {         screen: StackNavigator({Profile: {screen: ProfileScreen}}),       },     }, {       tabBarOptions: {         activeTintColor: '#e91e63',       },       navigationOptions: {         header: {           visible: true,         },       },     }); 
like image 426
MonkeyBonkey Avatar asked Feb 21 '17 00:02

MonkeyBonkey


People also ask

How do I use the react navigation header buttons?

The most common way to interact with a header is by tapping on a button either to the left or the right of the title. Let's add a button to the right side of the header (one of the most difficult places to touch on your entire screen, depending on finger and phone size, but also a normal place to put buttons).

How do you create a React Native header?

To configure the header bar of a React Native application, the navigation options are used. The navigation options are a static property of the screen component which is either an object or a function. headerTitle: It is used to set the title of the active screen. headerStyle: It is used to add style to the header bar.

How do I center my header in react navigation?

To center the header title, we need to have flex header by add flex property. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. headerTitleStyle: { alignSelf: 'center' , textAlign:"center",flex:1 }, Worked for me!


1 Answers

Even if this is a fairly old question, I had myself this question a couple days ago, so I'll be adding my two cents about it hoping this will be useful for someone else in the future as well.

React Navigation is an amazing product with a high amount of customization, but that also turns out sometimes to be confusing to begin with, which also applies to some sections of the documentation. navigationOptions as of the current version states, is common for all screens but the "list of available navigation options depends on the navigator the screen is added to." https://reactnavigation.org/docs/tab-navigator.html#navigationoptions-used-by-tabnavigator hence the header option doesn't work because it's not available for TabNavigator itself.

Regarding your question on which approach is the best that depends on what do you want to accomplish with the navigation for your app itself. If you put your TabNavigator inside a StackNavigator the header component will be common for all of the tabs inside the TabNavigator, meaning the tab transition will take effect but the header won't move from its top position. If you on the other hand choose to nest a StackNavigator inside every tab, the header will render inside every tab, meaning the header will move along the tab transition animation.

I made a quick demo for you to see the difference, the code is also available if you wanna play with it.

like image 117
Juan Carlos Alpizar Chinchilla Avatar answered Sep 21 '22 02:09

Juan Carlos Alpizar Chinchilla