import React from 'react' ; import analytics from '@react-native-firebase/analytics' ; import { NavigationState , NavigationContainer } from '@react-navigation/native' ; import { PartialState } from '@react-navigation/routers' ; import MainRoutes from './Main' ; function isNavigationState ( state ) { return 'index' in state ; } const getActiveRouteName = ( state ) : string => { const route = state . routes [ state . index ] ; const { state : childState } = route ; if ( childState && isNavigationState ( childState ) ) { return getActiveRouteName ( childState ) ; } return route . name ; } ; function isDefined ( state ) { return state !== undefined ; } const onNavigationStateChange = ( routeNameRef ) => ( prevState ) => { if ( ! isDefined ( prevState ) ) { return ; } const previousRouteName = routeNameRef . current ; const currentRouteName = getActiveRouteName ( prevState ) ; if ( previousRouteName !== currentRouteName ) { analytics ( ) . setCurrentScreen ( currentRouteName , currentRouteName ) ; } routeNameRef . current = currentRouteName ; } ; export default function LoggingRoutes ( ) { const routeNameRef = React . useRef ( null ) ; return ( < NavigationContainer ref = { routeNameRef } onStateChange = { onNavigationStateChange ( routeNameRef ) } > < MainRoutes /> < /NavigationContainer>); }