Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
How to add a customHeaderLeft button on a screen with goBack functionality using react-navigation in React Native?
<NavigationContainer>
  <Stack.Navigator screenOptions={{}}>
    <Stack.Screen name="home" component={Home} />
    <Stack.Screen
      name="MyProfile"
      component={Profile}
      options={{
        headerTintColor: 'white',
        title: 'My Profile',
        headerTransparent: true,
        headerShadowVisible: false,
        headerRight: () => (
          <TouchableOpacity>
            <DrawerIcon size={30} color={'white'} name="md-reorder-two-sharp" />
          </TouchableOpacity>
        ),
        headerLeft: () => (
          <TouchableOpacity>
            <Ionicons
              name="arrow-back-sharp"
              size={22}
              color="white"
              style={{ marginRight: 7, marginTop: 1, marginLeft: 3 }}
            />
          </TouchableOpacity>
        ),
      }}
    />
  </Stack.Navigator>
  <ModalPortal />
</NavigationContainer>

Below is the code I used for my stack navigation, and I want to go back from that screen

1.headerLeft: () => <TouchableOpacity>
  <Ionicons name="arrow-back-sharp" size={22} color="white" style={{ marginRight: 7, marginTop: 1, marginLeft: 3 }} />
  </TouchableOpacity>
}} />
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

There are two ways you can achieve this

1.) Set options On the Screen itself

You can use the useLayoutEffect hook to achieve this

On the screen where you want to put this header, i.e., the Profile screen just add the following code

React.useLayoutEffect(() => {
  navigation.setOptions({
    headerLeft: () => (
      <Ionicons
        name="arrow-back-sharp"
        size={22}
        color="white"
        style={{ marginRight: 7, marginTop: 1, marginLeft: 3 }}
        onPress={() => navigation.goBack()}
        // make sure you destructure the navigation variable from the props
        // or otherwise you'll have to write it like this
        // onPress={() => props.navigation.goBack()}
      />
    ),
  });
}, [navigation]);

And your Navigation Container should look like

<NavigationContainer>
  <Stack.Navigator screenOptions={{}}>
    <Stack.Screen name="home" component={Home} />
    <Stack.Screen
      name="MyProfile"
      component={Profile}
      options={{
        headerTintColor: 'white',
        title: 'My Profile',
        headerTransparent: true,
        headerShadowVisible: false,
      }}
    />
  </Stack.Navigator>
  <ModalPortal />
</NavigationContainer>;

Have a look at the Working Example Here

2.) Set the headerLeft and headerRight props in the Navigation Container

Setting the properties in the NavigationContainer, like this

<NavigationContainer>
  <Stack.Navigator screenOptions={{}}>
    <Stack.Screen name="home" component={Home} />
    <Stack.Screen
      name="MyProfile"
      component={Profile}
      options={({ navigation, route }) => ({
        headerTintColor: 'white',
        title: 'My Profile',
        headerTransparent: true,
        headerShadowVisible: false,
        headerRight: () => (
          <TouchableOpacity>
            <DrawerIcon size={30} color={'white'} name="md-reorder-two-sharp" />
          </TouchableOpacity>
        ),
        headerLeft: () => (
          <TouchableOpacity onPress={() => navigation.goBack()}>
            <Ionicons
              name="arrow-back-sharp"
              size={22}
              color="white"
              style={{ marginRight: 7, marginTop: 1, marginLeft: 3 }}
            />
          </TouchableOpacity>
        ),
      })}
    />
  </Stack.Navigator>
  <ModalPortal />
</NavigationContainer>

Have a look at the Working Example Here

about 4 years ago · Juan Pablo Isaza Denunciar

0

You have provided a TouchableOpacity as the back button but you haven't specified any onPress callback. You should provide an onPress callback to your TouchableOpacity.

<NavigationContainer>
  <Stack.Navigator screenOptions={{}}>
    <Stack.Screen name='home' component={Home} />
    <Stack.Screen 
      name='MyProfile'
      component={Profile} 
      options={({ navigation, route }) => ({
        headerTintColor: 'white',
        title: 'My Profile',
        headerTransparent: true,
        headerShadowVisible: false,
        headerRight: () => (
          <TouchableOpacity>
            <DrawerIcon size={30} color={'white'} name='md-reorder-two-sharp' />
          </TouchableOpacity>
        ),
        headerLeft: () => (
          <TouchableOpacity onPress={()=> navigation.goBack()}>
            <Ionicons name="arrow-back-sharp" size={22} color="white" style={{ marginRight: 7, marginTop: 1, marginLeft: 3 }} />
          </TouchableOpacity>
        )
      })} 
    />
  </Stack.Navigator>
  <ModalPortal />
</NavigationContainer>

If you just wanted to change the back button appearance only, You are better off with headerBackImage props of react-navigation stack.

Here's a Live Snack Example

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda