9

I'm using React Navigation 3.11 in my React Native app and I want to customize the back button in stack navigation. In the docs it says:

headerBackImage

React Element or Component to display custom image in header's back button. When a component is used, it receives a number of props when rendered (tintColor, title). Defaults to Image component with react-navigation/views/assets/back-icon.png back image source, which is the default back icon image for the platform (a chevron on iOS and an arrow on Android).

Here is my configuration:

let navigationRouteConfigMap : NavigationRouteConfigMap = {
      _main:
      {screen: page, navigationOptions: 
        { 
          [...]
          headerBackTitle: '',
          headerTruncatedBackTitle: '',
          headerBackImage: Images.backArrow,
        }
      }

    }
    let stackNavigatorConfig:StackNavigatorConfig = {
      headerBackTitleVisible: false
    }

    return createStackNavigator(navigationRouteConfigMap, stackNavigatorConfig)

I've got other pages as well in my config, all having the same shared navigation options with custom header back image. However, in my app it renders the default back arrow. (headerBackTitleVisible: false does work though)

What am I doing wrong?

5 Answers 5

9

this works too

headerBackImage: ()=>(<YourAsset />),
Sign up to request clarification or add additional context in comments.

1 Comment

thank you. this one solved the issue for me. very simple and straight forward.
6

Even you can use a custom component, in my case a SVG icon, looks something like this:

<LoginSignUpNav.Screen
    name="Login"
    component={Login}
    options={{
      headerBackImage: () => <BackIcon width={25} height={25} />,
      headerTintColor: "black",
      headerTransparent: true,
      headerStyle: {
         backgroundColor: 'transparent',
      },
    }}
/>

Comments

5

I was using an actual image (from require()) instead of a React Element. Also, for some reason the navigation options weren't picked up for individual pages either. I've switched to <Image.../> and set my object as defaultNavigationOptions in StackNavigatorConfig and it worked.

Comments

2

It might be because you are using Navigation 6.XX. To work it in 6.XX, try this out

headerLeft: () => (
   <TouchableOpacity
      style={styles.headerBtnContainer}
      onPress={() => props.navigation.goBack()}
   >
     <Ionicons name="close-sharp" size={24} color="white" />
   </TouchableOpacity>
),

This should work properly.

Comments

0

You can create your own Button with image

headerRight: (props) => (
      <Button
      {...props}
      onPress={() => { /*Do something */ }}
       />
  ),

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.