在 DrawerNavigator v.5 中重置嵌套的 StackNavigator

0 react-native react-navigation-drawer react-navigation-v5

在第 4 版中,我在 contentComponent 中使用 DrawerItems onItemPress 重置了 StackNavigation:

const AppDrawerNavigator = createDrawerNavigator(
  {
    Stack1: {
       screen: Stack1,
       navigationOptions: { // options}
   },
   Stack2: {
      screen: Stack2,
      navigationOptions: { // options}
   },
  {
    initialRouteName: 'Stack1',
    contentOptions: {
      activeTintColor: '#346bc7',
    },
    contentComponent: props => {
      return (
        <ScrollView>
          <SafeAreaView forceInset={{ top: 'always', horizontal: 'never' }} >
            <DrawerItems {...props} onItemPress={router => {
              const navigateAction = NavigationActions.navigate({
                routeName: router.route.routeName,
                params: {},
                action: NavigationActions.navigate({ routeName: router.route.routes[0].routeName }),
              });
              props.navigation.dispatch(navigateAction);
            }}
            />
          </SafeAreaView>
        </ScrollView>
      )
    }
  },
);
Run Code Online (Sandbox Code Playgroud)

React Navigation 5 有一个全新的 API,所以我的旧重置代码不起作用。我怎样才能在 DrawerNavigator v.5 中做到这一点?

Mor*_*oui 7

根据文档,这对我有用

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

 props.navigation.dispatch({
      ...CommonActions.reset({
        index: 0,
        routes: [{ name: "AuthStackNavigator" }]
      })
    });
Run Code Online (Sandbox Code Playgroud)

如果您想在该 Stacknavigator 中重置为特定屏幕,您可以这样做:

  props.navigation.dispatch({
      ...CommonActions.reset({
        index: 0,
        routes: [
          {
            name: "AuthStackNavigator",
            state: {
              routes: [
                {
                  name: "SignUpScreen",
                  params: {         //<--- send with param optional
                    paramTest: "param test"
                  }
                }
              ]
            }
          }
        ]
      })
    });
Run Code Online (Sandbox Code Playgroud)

我希望这对某人有帮助。