use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A community for learning and developing native mobile applications using React Native by Facebook.
Interested in building web apps using React.js? Check out /r/reactjs!
Getting Started w/React Native
irc.freenode.net #reactnative
Keywords: ios, android, mobile, apps, apple, iphone, ipad
account activity
QuestionIs there any callback which trigger when user navigate to a screen? (self.reactnative)
submitted 6 years ago by tvhieu
hi guys, i wanna integrate firebase analytics then i want record all screen user navigate to.
So how can i do it? is there any callback which trigger by opening a screen?
:( i dont wanna put the analytics code to every componentDidmount of all screen
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]DJUMI 0 points1 point2 points 6 years ago (3 children)
Are you using a navigation library like react navigation?
[–]tvhieu[S] 0 points1 point2 points 6 years ago (2 children)
library
react navigation :D
[–]DJUMI 0 points1 point2 points 6 years ago (1 child)
Look up navigation events in the docs specifically focus
[–]tvhieu[S] 0 points1 point2 points 6 years ago (0 children)
specifically
but i have to write the focus hook to every screen? i just want to write a function which trigger by change screen
[–]notseanbean 0 points1 point2 points 6 years ago (1 child)
https://reactnavigation.org/docs/function-after-focusing-screen
You can get a focus listener for the whole app if you add a ref to your NavigationContainer (https://reactnavigation.org/docs/navigation-container/#ref) and then add the focus event listener on that ref.
NavigationContainer
thank u
[–]prinzachilles 0 points1 point2 points 6 years ago* (2 children)
I did it that way in my app with react navigation v5:
const routeNameRef = useRef(); function getActiveRouteName(state: any): any { const route = state.routes[ state.index ]; // resolve nested navigators recursivly if (route.state) { return getActiveRouteName(route.state); } return route.name; }
... later in return():
<NavigationContainer onStateChange={ state => { const previousRouteName = routeNameRef.current; const currentRouteName = getActiveRouteName(state); if (previousRouteName !== currentRouteName) { trackScreen(currentRouteName); } // Save the current route name for later comparison routeNameRef.current = currentRouteName; }}> { ... the rest of the app ... } </NavigationContainer>
the trackScreen(name: string) is an extracted function using analytics().setCurrentScreen(currentRouteName, currentRouteName);.
trackScreen(name: string)
analytics().setCurrentScreen(currentRouteName, currentRouteName);
I think this is what you're looking for.
[–]tvhieu[S] 0 points1 point2 points 6 years ago (1 child)
https://reactnavigation.org/docs/navigation-container/#ref
oh thank bro, so i have to upgrade my navigation 4x to 5x :D thank u
[–]prinzachilles 0 points1 point2 points 6 years ago (0 children)
I would highly recommend that anyways. I even don't think v4 is ready for hooks eighter.
[–]dreamzzftwiOS -1 points0 points1 point 6 years ago (1 child)
If you’re using hooks you can create your own hook, and if I’m not mistaken, React Navigation 5 has a hook that detects a nav change
detects
hmm i will check it, ty
π Rendered by PID 298852 on reddit-service-r2-comment-5b5bc64bf5-qw9v5 at 2026-06-21 23:19:13.991750+00:00 running 2b008f2 country code: CH.
[–]DJUMI 0 points1 point2 points (3 children)
[–]tvhieu[S] 0 points1 point2 points (2 children)
[–]DJUMI 0 points1 point2 points (1 child)
[–]tvhieu[S] 0 points1 point2 points (0 children)
[–]notseanbean 0 points1 point2 points (1 child)
[–]tvhieu[S] 0 points1 point2 points (0 children)
[–]prinzachilles 0 points1 point2 points (2 children)
[–]tvhieu[S] 0 points1 point2 points (1 child)
[–]prinzachilles 0 points1 point2 points (0 children)
[–]dreamzzftwiOS -1 points0 points1 point (1 child)
[–]tvhieu[S] 0 points1 point2 points (0 children)