React native 使用react navigation重置TabNavigator历史记录

React native 使用react navigation重置TabNavigator历史记录,react-native,react-navigation,React Native,React Navigation,我的结构如下: const routeConfiguration = { Login: { screen: Login }, Home: { screen: TabBar }, }; const stackNavigatorConfiguration = { headerMode: 'screen', navigationOptions: { header: { visible: f

我的结构如下:

    const routeConfiguration = {
        Login: { screen: Login },
        Home: { screen: TabBar },
    };

    const stackNavigatorConfiguration = {
        headerMode: 'screen',
        navigationOptions: {
            header: { visible: false }
        }
    };

export const RootNav = StackNavigator(routeConfiguration, stackNavigatorConfiguration);
我的选项卡栏,其中每个选项卡都有自己的StackNavigator:

const routeConfiguration = {
    TabOneNavigation: { screen: TabOneNavigation },
    TabTwoNavigation: { screen: TabTwoNavigation },
    TabThreeNavigation: { screen: TabThreeNavigation },
};

const tabBarConfiguration = {
    tabBarOptions: {
        activeTintColor: 'white',
        inactiveTintColor: 'lightgray',
        labelStyle: {
            fontSize: 10,
            fontFamily: Fonts.book
        },
        style: {
            backgroundColor: Colors.greenLightGradient,
            borderTopWidth: 1,
            borderTopColor: Colors.tabGreenLine
        },
    }
};

export const TabBar = TabNavigator(routeConfiguration, tabBarConfiguration);
当应用程序首次加载时,它进入登录屏幕。成功登录后,我使用actionTypes.TO_HOME返回主页。在那里我有3个选项卡(提要、建议、配置文件)。在Profile选项卡内,我按下了Log Out(注销)按钮,在该按钮上我重置了导航历史并再次登录(到目前为止还不错)。但当我再次登录并返回主页时,它会显示第一个选项卡一秒钟,并将我导航到最后一个选项卡(配置文件),看起来TabNavigator的历史记录没有重置。我是否应该做一些特殊的事情,以便TabNavigator的历史记录也被重置

这是我的还原程序,用于重置历史记录并转到登录屏幕:

export const navReducer = (state = initialState, action = {}) => {
    let nextState;
    switch (action.type) {
        case actionTypes.TO_LOGIN:
            nextState = RootNav.router.getStateForAction(
                NavigationActions.reset({
                    index: 0,
                    actions: [NavigationActions.navigate({ type: NavigationActions.NAVIGATE, routeName: actionTypes.TO_LOGIN })],
                    key: null
                }), state);
            break;

        case actionTypes.TO_HOME:
            nextState = RootNav.router.getStateForAction(
                NavigationActions.reset({
                    index: 0,
                    actions: [NavigationActions.navigate({ type: NavigationActions.NAVIGATE, routeName: actionTypes.TO_HOME })],
                }), state);
            break;

        default:
            nextState = RootNav.router.getStateForAction(action, state);
            break;
    }

    return nextState || state;
};

您可以将索引0放在导航操作上,这是React Navigation v5示例,用于清除所有历史记录:

navigation.dispatch((state) => {
      return CommonActions.reset({
        ...state,
        history: state.history
          ? [
              state.history[
                state.history.length - 1
              ],
            ]
          : undefined,
      });
    });

它不起作用了。效果是一样的。我用更多的信息和代码更新了我的问题。你找到解决这个问题的方法了吗?