如何在反应导航中返回父屏幕?

How to go back to parent screen in react-navigation?

问题

RootTabNavigator
    | Auth Screen
    | Welcome Screen
    | MainTabNavigator 
              | FeedStacknavigator
                       | Screen A

              | MenuStackNavigator 
                       | Screen B <-I'm here and want to reset to 'Auth Screen'

树结构显示了我的应用程序的导航结构。我在 'Screen B' 并想返回到 Auth Screen。 (不导航)当我导航到 Auth Screen 时,它会创建新的 Auth Screen,我想避免它。

if ( ... ) {
      // I'm getting undefined error here
      nextProps.navigation.reset('Autho'); 

为什么它不起作用?

可能的解决方案 1:重置

  const resetAction = NavigationActions.reset({
      index: 0,
      actions: [
          // getting error because auth screen is not in MenuStacknavigator. 
          NavigationActions.navigate({ routeName: 'Auth'})
      ]
  })
  this.props.navigation.dispatch(resetAction);

可能的解决方案#2:

  const backAction = NavigationActions.back({
    key: 'Autho'
  })
  # Back Action doesn't do anything
  this.props.navigation.dispatch(backAction); # not working

可能的解决方案#3:

调度自定义操作。 <- 有一些针对 StackNavigator 的解决方案,但在我的例子中是 TabNavigator。

请随意回答。我现在找到了一个简单的解决方案:

const backAction = NavigationActions.back({
    key: null
})
nextProps.navigation.dispatch(backAction);

要重置为开始,我通常使用:

NavigationActions.reset({
    index: 0, 
    actions: []
)

顺便说一句,更好的导航结构是拥有多个选项卡导航器,您可以根据身份验证状态切换这些导航器。在您的情况下,这类似于 AuthTabNavigatorMainTabNavigator,您可以在它们之间切换。

希望对您有所帮助!