Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

Execute the same function for all screens when they get focused in with React Navigation

$
0
0

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!! :)


Viewing all articles
Browse latest Browse all 6287

Trending Articles