2

I want to clear all screen components and redirect it to login screen. Use case is:

Login -> 
MainScreen -> 
chose OrderModule from side menu ->
OrderScreen1->
OrderScreen2 ->
OrderScreen3 -> 
chose StockModule from side menu -> 
pressed device back button ->
reached to dashboard -> 
now pressed logout (internally uses `this.props.navigation.goBack(null)`) ->
I see OrderScreen2 instead of Login screen

My hierarchy is:

PrimaryNav (StackNavigator)
--Login
--MainScreen
--ResetPassword

MainScreen (StackNavigator)
--DrawerStack- DrawerScreen

DrawerScreen (DrawerNavigator)
--Dashboard
--OrderModule
--StockModule
--SalesModule

OrderModule (StackNavigator)
--OrderScreen1
--OrderScreen2
--OrderScreen3
Harish Gyanani
  • 1,366
  • 2
  • 22
  • 43
  • Possible duplicate of [React-Navigation with Login Screen](https://stackoverflow.com/questions/42876690/react-navigation-with-login-screen) – bennygenel Apr 30 '18 at 08:33

2 Answers2

1

This is working for me

this.props.navigation.reset([NavigationActions.navigate({ routeName: 'Login' })], 0)

reference https://reactnavigation.org/docs/en/navigation-prop.html

kamal verma
  • 496
  • 3
  • 16
0

I have found the solution for the above situation. I am posting it because I found it by hit and try approach and very useful in most of the scenarios for others.

Use `popToTop` method

this.props.navigation.popToTop();

use above line whenever module is clicked from drawer (side panel). It clears the old module stack then write the line to navigate to new screen or navigator. In my case : this.props.navigation.navigate('StockModule');

Harish Gyanani
  • 1,366
  • 2
  • 22
  • 43