0

I have a simple page with list of students. Once I click on each student profile, I want to go to a new page, where the title of the page will be the name of the student and where I will have a button on the right. All of this should be done in the header. So I put the following code:

<Stack.Screen name="Profile" component={ProfileScreen}
                  options={({ route }) => ({ title: route.params.name })}
 /> 

Which correctly displays the name of the student on the header. Then I wanted to add the button on the right and as written here I changed my code to:

<Stack.Screen name="Profile" component={ProfileScreen}
                  options={{ headerTitle: ({route}) => ({ title: route.params.name }),
                             headerRight: () => (
                                                <Button
                                                    onPress={() => alert('This is a button!')}
                                                    title="Info"
                                                    color="#fff"
                                                  />),
                          }}
              />

And now I have the following error: TypeError: undefined is not an object (evaluating 'route.params') Can someone advice how to add both custom title and button

2 Answers 2

1

You will have to pass it like below

   options={({ route }) => ({
      title: route.params.name,
      headerRight: () => (
        <Button
          onPress={() => alert('This is a button!')}
          title="Info"
          color="#fff"
        />
      ),
    })}
Sign up to request clarification or add additional context in comments.

Comments

1

There are two methods by which you can set up the custom header

  1. You can set up a custom button like this

     function LogoTitle() {
         return (
             <Image
                style={{ width: 50, height: 50 }}
                source={require('@expo/snack-static/react-native-logo.png')}
             />
         );
     }
    
     function StackScreen() {
         return (
              <Stack.Navigator>
                  <Stack.Screen
                       name="Home"
                       component={HomeScreen}
                       options={{ headerTitle: props => <LogoTitle {...props} /> }}
                  />
              </Stack.Navigator>
         );
     }
    
  2. You can hide the header by navigation

     options: {
         header: null,
     }
    

    and can also create custom header and import it to any component you can pass props from every component to that one custom header

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.