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

136
Vistas
Execute the same function on each screen for all screens when they get focused in with React Navigation

How can I execute the same function on each screen with React Navigation? I'm using React Native and TypeScript. I have a navigation with home, profile, etc...

I have the below function and I want it to be executed when you navigate to profile, home, etc.

const getFunction = (): void => {
  console.log("execute function");
};

I tried the below code but it doesn't work:

useEffect(() => {
  const listener = navigation.addListener("focus", (e) => {
    getFunction();
  });
  return () => listener;
}, [navigation]);

An overview of my screens:

return (
  ...
        <Stack.Screen
          name='home'
          component={home}
         />
         <Stack.Screen
           name='profile'
           component={profile}
         />
    ...
 )

Please if you could help me I would really appreciate it!! :)

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can use screenListeners prop on the navigator. This way you can specify listeners for events from all screens. As example like so:

<Stack.Navigator
  screenListeners={{
    focus: (e) => {
      // code you wanna execute goes here
      console.log("Focus has changed")
    },
  }}
>
  <Stack.Screen name="home" component={home} />
  <Stack.Screen name="profile" component={profile} />
</Stack.Navigator>
about 4 years ago · Santiago Trujillo 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