react-native获取当前路线名称

时间:2018-06-29 14:30:47

标签: react-native react-navigation

这是“分享您的知识”的帖子。我需要此功能并搜索了许多小时,因此我分享了我的解决方案。随时发布CC。

如何在React Native中从主应用获取当前路线名称?

编辑:这是用于反应导航V1

(知道我们不能在其中使用this.props.navigation,因为它定义了TabNavigator和StackNavigator本身)

OnInit

我需要这样在用户每次单击时都在每个TabNavigator屏幕上显示教程模式(直到他单击“我明白了”)

1 个答案:

答案 0 :(得分:2)

在官方文档中使用场景跟踪方法detailed here

function getActiveRouteName(navigationState) {
  if (!navigationState) {
    return null;
  }
  const route = navigationState.routes[navigationState.index];
  // dive into nested navigators
  if (route.routes) {
    return getActiveRouteName(route);
  }
  return route.routeName;
} 

<StackView
  onNavigationStateChange={(prevState, currentState) => {
    const currentScreen = getActiveRouteName(currentState);
    console.log("currentScreen  : " + currentScreen);  
  }}
/>
相关问题