1

How do you implement a custom header component in React Navigation 5?

There is a variable for header, but assigning a component to it doesn't show anything, do you need to assign headerLeft, headerTitle and headerRight for every screen?

        <Stack.Screen name="IndexScreen" component={IndexScreen}
          options={{
            header: () => <HeaderComponent /> 
          }} /> //shows nothing

        <Stack.Screen name="IndexScreen" component={IndexScreen}
          options={{
            headerLeft: () => <HeaderLeftComponent />, 
            headerTitle: () => <HeaderTitleComponent />, 
            headerRight: () => <HeaderRightComponent /> // to much repetition
          }} /> 
          
4
  • What does your HeaderComponent look like, because it's working for me. Creating a snack might also be a good idea, to make it easier to debug. Commented Jul 30, 2020 at 11:46
  • @Bas I managed to get a custom component to work as a header and made a snack: snack.expo.io/@johanmelin/4c7d80. Now I'm just curious how I can get all the header logic into this component, like headerRight: () => <View> Commented Jul 30, 2020 at 12:37
  • Simple creating an object with the settings doesn't work <Stack.Screen options={{ HeaderSettings }} /> const HeaderSettings = { header: HeaderComponent } Commented Jul 30, 2020 at 13:29
  • I can get both settings and header component to work, but not both at the same time. options={{ ...HeaderSettings, header: props => <HeaderComponent {...props} />, }} Commented Jul 30, 2020 at 14:20

1 Answer 1

2

You can use the header attribute in screenOptions prop from Stack.Navigator (if it is a common header):

<Stack.Navigator
  screenOptions={{
    header: YourHeader
  }}
>
  // ...
</Stack.Navigator>

Or header attribute in options from Stack.Screen for each screen:

<AppStack.Screen
  name='YourScreen'
  component={YourComponent}
  options={{ header: YourHeader }}
/>

If your header is not showing up, probably the Screen is rendering something with a higher zIndex or elevation.


The docs says which props the header component receives, but you can also check it on the types file (on GitHub), search for StackHeaderProps.

Sign up to request clarification or add additional context in comments.

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.