1

在版本 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>
      )
    }
  },
);

React Navigation 5 有一个全新的 API,所以我的旧重置代码不起作用。如何在 DrawerNavigator v.5 中执行此操作?

4

2 回答 2

8

根据文档,这对我有用:

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

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

如果你想重置到 Stacknavigator 中的特定屏幕,你可以这样做:

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

我希望这对某人有所帮助。

于 2020-06-06T11:24:49.703 回答
0

您在 react-navigation-v5 简单代码中使用抽屉内容而不是 contentComponent;

<Drawer.Navigator drawerContent={props => <DrawerCustomComponent {...props} />}>
     {{ screen objects }}
</Drawer.Navigator>

样板工程;github

于 2020-03-05T19:20:06.350 回答