7

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' })
  });
Hrishi
  • 1,210
  • 1
  • 13
  • 25

3 Answers3

18

In React Navigation 5.x

use unmountOnBlur

<Drawer.Screen
    name={...}
    component={...}
    unmountOnBlur={true}
    options={{unmountOnBlur: true}}
/>
Community
  • 1
  • 1
Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
7

put this in navigationOptions of drawer navigator

unmountInactiveRoutes: true
Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
Daniyal Awan
  • 256
  • 3
  • 13
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".