Reactjs 如何重置嵌套导航器(react navigation v5)

Reactjs 如何重置嵌套导航器(react navigation v5),reactjs,react-native,react-navigation,react-navigation-stack,react-navigation-v5,Reactjs,React Native,React Navigation,React Navigation Stack,React Navigation V5,有两套堆栈导航器 const SetOneScreens = () => ( <Stack.Navigator initialRouteName="AScreen"> <Stack.Screen name="AScreen" component={AScreen} /> <Stack.Screen name="BScreen" component={BScreen} />

有两套堆栈导航器

const SetOneScreens = () => (
  <Stack.Navigator initialRouteName="AScreen">
    <Stack.Screen name="AScreen" component={AScreen} />
    <Stack.Screen name="BScreen" component={BScreen} />
  </Stack.Navigator>
);
const SetTwoScreens = () => (
  <Stack.Navigator initialRouteName="CScreen">
    <Stack.Screen name="CScreen" component={CScreen} />
    <Stack.Screen name="DScreen" component={DScreen} />
  </Stack.Navigator>
);
如何使用
导航
通用操作

处理
重置
,如react-navigation-v5中所述,您需要使用重置操作分派CommonAction以清除应用程序的后堆栈,这样当用户按下设备的硬件后退按钮时,应用程序不会返回到上一屏幕,检查下面的示例

import { CommonActions } from "@react-navigation/native";

props.navigation.dispatch({
  ...CommonActions.reset({
    index: 0,
    routes: [{ name: "AnotherStackNavigator" }]
  })
});
或者,如果您想在StackNavigator中重置到特定屏幕,可以执行以下操作:

props.navigation.dispatch({
  ...CommonActions.reset({
    index: 0,
    routes: [
      {
        name: "AnotherStackNavigator",
        state: {
          routes: [
            {
              name: "AnotherStackNavigatorScreen",
              params: {         
                  ...
              }
            }
          ]
        }
      }
    ]
  })
});

在文档中很难找到您的解决方案!你救了我一天。。。
import { CommonActions } from "@react-navigation/native";

props.navigation.dispatch({
  ...CommonActions.reset({
    index: 0,
    routes: [{ name: "AnotherStackNavigator" }]
  })
});
props.navigation.dispatch({
  ...CommonActions.reset({
    index: 0,
    routes: [
      {
        name: "AnotherStackNavigator",
        state: {
          routes: [
            {
              name: "AnotherStackNavigatorScreen",
              params: {         
                  ...
              }
            }
          ]
        }
      }
    ]
  })
});