Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

How to get current navigation state react-navigation V4 React-Native

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...