Search code examples
react-nativereact-native-navigationreact-navigation-stack

Is there anyway to turn `options` into a function same like `navigationOptions` do?


Currently, I was taking a course:Multiplatform Mobile App Development with React Native in coursera, and I was stuck at after every lecture because the instructor use react-navigation@2.0.1 but I want to make sure to learn the latest version(v5). In this lecture he created a stack navigator and bring an icon to a screen like,

import {createStackNavigator} from 'react-navigation';
import { Icon } from 'react-native-elements';

const MenuNavigator = createStackNavigator(
  {
    Menu: {
      screen: Menu,
      navigationOptions: ({ navigation }) => ({
        headerLeft: (
          <Icon
            name="menu"
            size={24}
            color="white"
            onPress={() => navigation.toggleDrawer()}
          />
        ),
      }),
    },
    Dishdetail: { screen: Dishdetail },
  },
  {
    initialRouteName: 'Menu'
  }
);

Where navigationOptions can be an object or be a function that takes in props.

I convert it like,

import { createStackNavigator } from '@react-navigation/stack';
import { Icon } from 'react-native-elements';

const MenuNavigator = createStackNavigator();
function MenuNavigatorScreen() {
  return (
    <MenuNavigator.Navigator
      initialRouteName="Menu"
      screenOptions={HeaderOptions}
    >
      <MenuNavigator.Screen
        name="Menu"
        component={Menu}
      />
      <MenuNavigator.Screen
        name="Dishdetail"
        component={Dishdetail}
        options={{ headerTitle: 'Dish Detail' }}
      />
    </MenuNavigator.Navigator>
  );
}

But I was confused how to convert the navigationOptions functionality into my code. Because their docs didn't tell how to trun my options object into a function to bring the navigation prop?

One more thing is he was using drawerIcon,

const MainNavigator = createDrawerNavigator(
  {
      navigationOptions: {
        drawerLabel: 'Login',
        drawerIcon: ({ tintColor }) => (
          <Icon
            name="sign-in"
            type="font-awesome"
            size={24}
            color={tintColor}
          />
        ),
      }
...

But I didn't find anything related drawerIcon in Drawer navigation docs
I heartily thank if anyone helps me to figure out this.


Solution

  • First of all, The options prop can be used to configure individual screens inside the navigator. And headerLeft is a function that returns a React element to display on the left side of the header. When a function is used, it receives several arguments when rendered (onPress, label, labelStyle, and more - check types.tsx for the complete list).

    options = {
        ({
            navigation
        }) => ({
            headerLeft: () => ( <
                Icon name = 'menu'
                size = {
                    24
                }
                color = 'white'
                onPress = {
                    () =>
                    navigation.toggleDrawer()
                }
                />
            )
    
        })
    }
    

    And for drawerIcon use:

    options = {
        {
            drawerIcon: ({
                tintColor
            }) => ( <
                Icon name = 'home'
                type = 'font-awesome'
                size = {
                    24
                }
                color = {
                    tintColor
                }
                />
            )
        }
    }