如何从导航位置识别屏幕。 React Native中的DrawerNavigation或StackNavigation

时间:2018-12-01 09:27:34

标签: reactjs react-native react-navigation

我有一种情况,我正在使用页脚标签(堆栈)以及左侧菜单(抽屉)中的 screen1 。我添加了300ms的延迟,这对于抽屉式导航是很好的,因为屏幕会等到抽屉关闭,但是在页脚选项卡中会应用相同的延迟,因此我需要覆盖该延迟。

是否有任何方法可以标识在 transitionConfig 下使用了哪种导航?这是我尝试修改的代码,但不能解决

navigationOptions: {
  transitionConfig: (toTransitionProps, fromTransitionProps) => {
    const isBack = fromTransitionProps.navigation.state.index >= toTransitionProps.navigation.state.index;
    const routeName = isBack ? fromTransitionProps.scene.route.routeName : toTransitionProps.scene.route.routeName;

    // This check is only for the case where the transitionConfig is specified globally per navigator basis
    // If the config is specified per screen basis, then `routeName` will always refer to the current screen
    if (routeName === 'Search') {
      return {
        transitionSpec: { duration: isBack ? 150 : 200 },
        screenInterpolator: CardStackStyleInterpolator.forFade(props),
      }
    }
  },
}

引自Github Issue

1 个答案:

答案 0 :(得分:0)

the code中可以看到可以访问导航状态。这意味着您可以在调用navigate(或push)时传递参数,以指定哪个元素调用了导航,然后可以在导航状态下读取该元素。

toTransitionProps.navigation.getParam('invokedBy',/*default value*/)