8

我有一个已放入堆栈的登录屏幕。用户成功登录后,他被重定向到主屏幕,这是一个抽屉屏幕。抽屉屏幕的选项之一是注销,因此单击它的用户应该注销。以下是我的注销屏幕代码。我只是在 ui 中显示注销屏幕的进度条,但在useEffect挂钩中,我正在调用以下方法

navigation.navigate({index: 0, routes: [{name: LOGIN_SCREEN}]});

但我收到一条错误消息You need to specify name or key when calling navigate with an object as the argument,我被重定向到主屏幕。当我完全重新启动我的应用程序时,它只会移动到登录屏幕。我正在为名称键传递正确的值。

我的导航堆栈如下所示

 <Stack.Navigator>

      <Stack.Screen
        name={LOGIN_SCREEN}
        component={LoginScreen}
        options={{headerShown: false}}
      />
     <Stack.Screen
        name={HOME_STACK_SCREEN}
        component={DrawerStack}
        options={{headerShown: false}}
      />...

我的抽屉组件如下

<Drawer.Navigator
      drawerStyle={{backgroundColor: BLUE_COLOR_1}}
      drawerContentOptions={{labelStyle: {color: '#FFF'}}}>
      <Drawer.Screen
        name={HOME_SCREEN}
        component={Home}
        options={{
         ...
        }}
      />
     <Drawer.Screen
        name={LOGOUT_SCREEN}
        component={Logout}
        options={{
         ...
        }}
      />
4

2 回答 2

16

在反应导航 v5. 您可以像这样重置导航

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

this.props.navigation.dispatch(
     CommonActions.reset({
        index: 0,
        routes: [{ name: "LOGIN_SCREEN" }],
    })
);
于 2020-04-24T15:53:28.317 回答
14

如果要重置,则需要使用reset,而不是navigate

navigation.reset({
  routes: [{ name: LOGIN_SCREEN }]
});
于 2020-02-29T21:18:59.987 回答