我试图了解如何在嵌套堆栈中重置
这是我的代码

    const AuthStack = createStackNavigator(
      {
        Welcome,
        Login,
        Register,
        ConfirmationCode,
      },
      {
        initialRouteName: 'Welcome',
        headerMode: 'none',
        lazy: true,
        transitionConfig,
        defaultNavigationOptions: {
          gesturesEnabled: false,
        },
      }
    )

    const AppStack = createStackNavigator(
      {
        TabStack,
        SearchResult,
        BusinessDetail,
        BusinessMap,
        MakeAppointment,
        TermsAndConditions
      },
      {
        initialRouteName: 'TabStack',
        headerMode: 'none',
        lazy: true,
        transitionConfig,
        defaultNavigationOptions: {
          gesturesEnabled: false,
        },
      }
    )

    let MainStack = createSwitchNavigator(
      {
        AuthLoading,
        Auth: AuthStack,
        App: AppStack,
      },
      {
        initialRouteName: 'AuthLoading',
        headerMode: 'none',
        lazy: true,

        defaultNavigationOptions: {
          gesturesEnabled: false,
        },
      }
    )

标签栈
    import React from 'react';

    import { createBottomTabNavigator, createAppContainer } from 'react-navigation';
    import {
        Search,
        MyFavourites,
        MyAppointments,
        UserProfile
    } from '../screens'
    import Icon from 'react-native-vector-icons/Feather';
    import Colors from '../utils/Colors'
    let TabStack = createBottomTabNavigator(
      {
        Search,
         MyFavourites,
         MyAppointments,
         UserProfile,
      },
        initialRouteName: 'ScreenTab1',
        tabBarOptions: {
          activeTintColor: Colors.pink,
          inactiveTintColor: Colors.black,
          showLabel: false,
          style: {
            backgroundColor: 'white'
          }
        },
      }
    )
    export default createAppContainer(TabStack);

我想了解例如如何进行重置:
    reset from UserProfile to TabStack (in AppStack) to AuthStack

我试图以此方式做
const resetAction = StackActions.reset({
        index: 0,
        actions: [NavigationActions.navigate({ routeName: 'AuthStack' })],
    });
    this.props.navigation.dispatch(resetAction);

或这样
const resetAction = StackActions.reset({
        index: 0,
        key: null,
        actions: [NavigationActions.navigate({ routeName: 'AuthStack' })],
    });
    this.props.navigation.dispatch(resetAction);

但我得到了错误



我检查了stackoverflow中的问题,但是那里的答案对我不起作用,总是向我展示我上面写的相同错误。

最佳答案

您的resetAction不成功,因为您是在TabStack上调度它的(因为如果我正确理解的话,因为您是在this.props.navigation.dispatch上调用UserProfile的)。您需要将resetAction调度到您的MainStackThis thread here提出了一些可以实现此目标的方法。而且,这是我的首选解决方案,因为我不必在导航器周围传递 Prop ,也不必为此调用多个嵌套 Action 。

  • 使用以下内容创建navigationService.js(以使您的顶级导航器作为引用)

  • import { NavigationActions, StackActions } from 'react-navigation';
    
    let _navigator;
    
    function setTopLevelNavigator(navigatorRef) {
      _navigator = navigatorRef;
    }
    
    function navigateMainNavigator(routeName, params) {
      _navigator.dispatch(
        NavigationActions.navigate({
          routeName,
          params,
        }),
      );
    }
    
    // add other navigation functions that you need and export them
    
    export default {
      setTopLevelNavigator,
      navigateMainNavigator,
    };
    
  • App.js或呈现MainStack的任何其他文件上,执行此操作以设置引用

  • import NavigationService from './navigationService';
    
    ...
    
    render() {
      return (
    
        ...
        <MainStack
          ref={navigatorRef => {
            NavigationService.setTopLevelNavigator(navigatorRef);
          }}
        />
        ...
    
      )
    }
    
  • 并且无论何时要重置为AuthStack(或MainStack中的任何其他堆栈),只需导入NavigationService并调用

  • NavigationService.navigateAndReset('Auth', {...yourParamsIfAny});
    // 'Auth' because you named it that way in your 'MainStack'
    

    ================================================== =========================

    已编辑
    navigationService.js中的先前解决方案是将StackNavigator作为MainStack
    function navigateAndReset(routeName, params) {
      _navigator.dispatch(
        StackActions.reset({
          index: 0,
          actions: [
            NavigationActions.navigate({
              routeName,
              params,
            }),
          ],
        })
      );
    }
    

    10-08 12:24