当您从另一个选项卡返回选项卡时如何重置选项卡堆栈反应导航v5

Vin*_*eel 9 react-native react-navigation react-navigation-stack react-navigation-bottom-tab

我有 3 个选项卡,每个选项卡都包含一组堆栈导航器。

  1. 主堆栈
    const HomeNavigator = createStackNavigator();

    const HomeStackNavigator = ({navigation, route}) => {
      return (
        <HomeNavigator.Navigator>
          <HomeNavigator.Screen
            name="Home"
            component={Home}
          />
          <HomeNavigator.Screen
            name="Profile"
            component={Profile}
          />
          <HomeNavigator.Screen
            name="Settings"
            component={Settings}
          />
        </HomeNavigator.Navigator>
      );
    };

Run Code Online (Sandbox Code Playgroud)
  1. 存储堆栈

    const StoreNavigator = createStackNavigator();

    const StoreStackNavigator = ({navigation, route}) => {
      return (
        <StoreNavigator.Navigator>
          <StoreNavigator.Screen
            name="OurStore"
            component={Store}
          />
        </StoreNavigator.Navigator>
      );
    };

Run Code Online (Sandbox Code Playgroud)
  1. 社区堆栈
    const CommunityNavigator = createStackNavigator();

    const CommunityStackNavigator = ({navigation, route}) => {
      return (
        <CommunityNavigator.Navigator>
          <CommunityNavigator.Screen
            name="Community"
            component={Community}
          />
          <CommunityNavigator.Screen
            name="CommunityReply"
            component={CommunityReply}
            options={communityReplyOptions}
          />
          <CommunityNavigator.Screen
            name="AskCommunity"
            component={AskCommunity}
          />
        </CommunityNavigator.Navigator>
      );
    };

Run Code Online (Sandbox Code Playgroud)

选项卡导航器


    const MainNavigator = createBottomTabNavigator();

    const MainTabNavigator = () => {
      return (
        <MainNavigator.Navigator
          screenOptions={tabScreenOptions}
          tabBarOptions={tabBarOptions}>
          <MainNavigator.Screen
            name="HomeTab"
            component={HomeStackNavigator}
            options={{tabBarLabel: 'Home'}}
          />
          <MainNavigator.Screen
            name="StoreTab"
            component={StoreStackNavigator}
            options={{tabBarLabel: 'Store'}}
          />
          <MainNavigator.Screen
            name="CommunityTab"
            component={CommunityStackNavigator}
            options={{tabBarLabel: 'Community'}}
          />
        </MainNavigator.Navigator>
      );
    };

Run Code Online (Sandbox Code Playgroud)

我通过使用以下方法单击按钮导航到 HomeTab 中 CommunityTab 选项卡内的 CommunityReply 屏幕

props.navigation.navigate('CommunityTab', { screen: 'CommunityReply', params: {postId: postId}, });

它工作正常,当我再次返回 CommunityTab 时,它将始终位于 CommunityReply 屏幕中。返回 CommunityTab 选项卡时如何重置选项卡堆栈

React 导航版本

“@react-navigation/bottom-tabs”:“^5.8.0”

“@react-navigation/native”:“^5.7.3”

“@react-navigation/stack”:“^5.9.0”

Eti*_*ier 27

有一个名为 unmountOnBlur 的属性就是为此目的而设计的。

https://reactnavigation.org/docs/bottom-tab-navigator#unmountonblur

在选项卡导航器中进行的更改:

const MainNavigator = createBottomTabNavigator();

const MainTabNavigator = () => {
  return (
    <MainNavigator.Navigator
-     screenOptions={tabScreenOptions}
+     screenOptions={{...tabScreenOptions, unmountOnBlur: true }}
      tabBarOptions={tabBarOptions}>
      <MainNavigator.Screen
        name="HomeTab"
        component={HomeStackNavigator}
        options={{tabBarLabel: 'Home'}}
      />
      <MainNavigator.Screen
        name="StoreTab"
        component={StoreStackNavigator}
        options={{tabBarLabel: 'Store'}}
      />
      <MainNavigator.Screen
        name="CommunityTab"
        component={CommunityStackNavigator}
        options={{tabBarLabel: 'Community'}}
      />
    </MainNavigator.Navigator>
  );
};
Run Code Online (Sandbox Code Playgroud)

摘要:
unmountOnBlur: true将解决提到的问题。

  • 简单得惊人。这正是我花了几个小时在谷歌上搜索如何重置嵌套堆栈导航器等后所寻找的。它也适用于抽屉导航器。 (2认同)
  • 最好的解决方案,谢谢! (2认同)

小智 5

如果您不希望CommunityReply在导航回 时显示屏幕,则CommunityTab需要在函数initial中添加一个选项navigate

props.navigation.navigate(
  'CommunityTab', 
  { 
    screen: 'CommunityReply', 
    initial: false, 
    params: { postId: postId }, 
  }
);
Run Code Online (Sandbox Code Playgroud)

文档中对此进行了解释