Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Title not working on react-navigation createStackNavigator

I'm using createStackNavigator inside createBottomTabNavigator from react-navigation in my app. I want to have a title on my screen. Following React Navigation's tutorial I've implemented it this way:

createBottomTabNavigator(
    {
      Home: createStackNavigator(
       {screen: HomePage, navigationOptions: () => { title: 'Home'}}),
      ...
    },

However, nothing is displayed in the navigation bar. I've also tried headerTitle though no avail.

What am I doing wrong?

like image 358
Can Poyrazoğlu Avatar asked Apr 26 '19 13:04

Can Poyrazoğlu


People also ask

How do I add a title in React Native?

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.

What is Createstacknavigator?

Provides a way for your app to transition between screens where each new screen is placed on top of a stack. By default the stack navigator is configured to have the familiar iOS and Android look & feel: new screens slide in from the right on iOS, fade in from the bottom on Android.


2 Answers

There are 2 ways of setting navigationOptions, object or function

Object

{
    screen: HomePage,
    navigationOptions: { title: 'Home' }
}

Function that return an object

{
    screen: HomePage,
    navigationOptions: ({ navigation }) => {
       return { title: 'Home' }
    }
}

Your code doesn't work is due to you have error in your arrow function, you should add a bracket around the body so that it returning the object.

{ screen: HomePage, navigationOptions: () => ({ title: 'Home'}) }
like image 194
Ewe Tek Min Avatar answered Oct 20 '22 13:10

Ewe Tek Min


navigationOptions shouldn't be a function, instead, is a JSON. So, remove the arrows and do it like this:

createBottomTabNavigator(
{
  Home: createStackNavigator(
   {screen: HomePage, navigationOptions: { title: 'Home'},
  ...
},
like image 1
SmoggeR_js Avatar answered Oct 20 '22 11:10

SmoggeR_js