Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Navigation v5 won't reset and remove previous routes

I have a login screen which I have placed in stack. After user logs in successfully he is redirected to home screen which is a drawer screen. One of the options of drawer screen is logout, so on click of it user should be logged out. Following is my code for logout screen. I am just showing a progress bar of logout screen in ui but in useEffect hook, I am calling the following method

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

but I get an error saying You need to specify name or key when calling navigate with an object as the argument and I am redirected to home screen. when I restart my app completely then only it moves to login screen. I am passing the right value for name key.

My Navigation stack looks something as follows

 <Stack.Navigator>

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

My drawer component as follows

<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={{
         ...
        }}
      />
like image 407
Pritish Avatar asked Feb 29 '20 14:02

Pritish


People also ask

What is replace in navigate react?

The replace action allows to replace a route in the navigation state. It takes the following arguments: name - string - A destination name of the route that has been registered somewhere.

How to handle the reset with navigation or commonactions?

How should I handle the reset with navigation or CommonActions As written in the documentation of react-navigation-v5, you need to dispatch CommonAction with reset-action to clear back-stack of your application, so that application doesn't go back to previous screen when user press hardware back-button of device, check below example,

Is it possible to reset the navigation state of a navigator?

But this is going to reset your navigation state with completely new state, unmounting and remounting some screens. You can keep the old routes like shown in dispatch docs but it only applies to current navigator, not child navigator. Here, it seems like you just want navigate, not reset.

How do I use navigation actions in a pop stack?

Whenever you navigate simply add the type for example: const navAction = NavigationActions.navigate ( { routeName: 'SomeScreenToGoToo', type: 'PopStack', params: { transition: SlideInFromRight } }); navigation.dispatch (navAction); // Called when the navigator needs the next state.


2 Answers

In react-navigation v5. You can reset navigation like this

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

this.props.navigation.dispatch(
     CommonActions.reset({
        index: 0,
        routes: [{ name: "LOGIN_SCREEN" }],
    })
);
like image 61
Shahnawaz Hussain Avatar answered Oct 13 '22 04:10

Shahnawaz Hussain


If you want to reset then you need to use reset, not navigate:

navigation.reset({
  routes: [{ name: LOGIN_SCREEN }]
});
like image 39
satya164 Avatar answered Oct 13 '22 03:10

satya164