6

I am using react navigation v3 in my app, I use stack navigator inside drawer navigator ,On the click of logout I navigate to login screen with clearing storage of user, But whenever I login again , Main component dose not call componentWillMount or componentDidMount method , and displays Previously loaded data on it. here is my code >

const screens = {
  login: { screen: Login },
  dashboard: { screen: Dashboard },
  patientList:{screen:StackNav},
  headerComponent:HeaderComponent
 }

  const MyDrawerNavigator = createDrawerNavigator(
     screens,
      {
       initialRouteName: 'login',
      contentComponent: Sidebar
       }
     );

    App  = createAppContainer(MyDrawerNavigator);
    export default App;

StackNav ==

export default createStackNavigator({
     PatientList,
     PatientDetails
 });

Logout Function ==

  localStorageService.removeAllKeys().then((res) => {
    this.props.navigation.navigate(route, { isLogin: 'N' })
  });
0

3 Answers 3

16

In React Navigation 5.x

use unmountOnBlur

<Drawer.Screen
    name={...}
    component={...}
    unmountOnBlur={true}
    options={{unmountOnBlur: true}}
/>
2
  • 2
    what if i dont need a drawer and just want to use stackNavigator? Nov 22, 2020 at 23:01
  • only this part is sufficient options={{unmountOnBlur: true}}
    – Anwar Gul
    Aug 29, 2021 at 16:57
7

put this in navigationOptions of drawer navigator

unmountInactiveRoutes: true
2
  • 1
    That doesn't work for me. Using in a component created with createDrawerNavigator. Sep 9, 2019 at 0:11
  • It works for me. Thanks! Using react-navigation 5.1.0 (via Expo)
    – Digo Gomes
    May 16, 2020 at 4:08
0

As Daniyal's awnser complementation: I've introduced the configuration inside createDrawerNavigator() configs as above:

const AppNavigator = createDrawerNavigator(
{
  Home:{
    screen: Home
  },
  Login:{
    screen: Login,
    navigationOptions: {
        drawerLabel: () => null
    }
  }
},
{
  unmountInactiveRoutes: true
});

And now all pages are working withouth any "cache".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.