I am using React Navigation's Stack Navigator from https://reactnavigation.org/docs/**4.x**/screen-tracking, but this works only in the case of onNavigationStateChange. Initially, when the application is launched, I want to fetch the Screen Name / Current Component Name
import { createAppContainer, createStackNavigator } from 'react-navigation';
function getActiveRouteName(navigationState) {
if (!navigationState) {
return null;
}
const route = navigationState.routes[navigationState.index];
if (route.routes) {
return getActiveRouteName(route);
}
return route.routeName;
}
const AppNavigator = createStackNavigator(AppRouteConfigs);
const AppContainer = createAppContainer(AppNavigator);
export default class App extends React.Component {
componentDidMount() {
/********Getting the initial route name here**********/
}
render() {
return (
<AppContainer
onNavigationStateChange={(prevState, currentState, action) => {
const currentRouteName = getCurrentRouteName(currentState);
const previousRouteName = getCurrentRouteName(prevState);
if (previousRouteName !== currentRouteName) {
console.log('Current Component is - ' + currentRouteName);
}
}}
/>
);
}
}
Can someone help in getting the current screen or component name in the initial launch for version 4.x
question from:
https://stackoverflow.com/questions/65950909/how-to-get-current-navigation-state-react-navigation-v4-react-native 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…