0

I have screens in stacknavigator which are nested inside DrawerNavigator. I wanted to restart the screens in DrawerNavigator when clicked.

System flow Architecture:

App.js::

StackNavigator:

  1. Login.js
  2. ScreensSetup.js

ScreensSetup.js consists the following::

a. Stack screens as:

  const FirstStackNavigator = createStackNavigator({
    First: {
      screen: Dashboard,
    },
  });

  const SecondStackNavigator = createStackNavigator({
    Second: {
      screen: Workorders,
    },
  });

b. DrawerNavigator nested with above stacked screens as:

  const DrawerNavigator = createDrawerNavigator({
    Dashboard: {
      screen: FirstStackNavigator,
    },
    Workorders: {
      screen: SecondStackNavigator,
    },
  },
  {
    contentComponent: DrawerContent ,
    initialRouteName: 'Dashboard',
  });

I am able to navigate to "Workorders" from "ScreensSetup" by:

  this.props.navigation.navigate('Workorders');

This works perfectly.

But I need to restart the screen. So, I used: For reseting ScreensSetup:

const navigateAction = StackActions.reset({
    index: 0,
    actions: [NavigationActions.navigate({ routeName: "ScreensSetup" })],
  });

  this.props.navigation.dispatch(navigateAction);

This works perfectly.

For reseting Workorders:

const navigateAction = StackActions.reset({
    index: 0,
    actions: [NavigationActions.navigate({ routeName: "Workorders" })],
  });

  this.props.navigation.dispatch(navigateAction);

This gives me error:

 Error: Error: There is no route defined for key Workorders.                        
    │ Must be one of: 'Login','ScreensSetup'

I simply wanted to reset the screen when clicked on menus on drawer naviagtor. I am completely messed up with this navigation. Any help would be appreciated. Thank You.

Kamalesh kunwar
  • 262
  • 1
  • 5
  • 20
  • Possible duplicate of [How to go to initialRoute with react-navigation when exiting a child stack navigator inside a drawer?](https://stackoverflow.com/questions/57063950/how-to-go-to-initialroute-with-react-navigation-when-exiting-a-child-stack-navig) – remeus Jul 31 '19 at 08:38
  • @Remeus This does not helped me. I wanted to reset the menu's screen of drawer navigator which has been pressed. It just resumes now. How can it be reset ? – Kamalesh kunwar Aug 01 '19 at 06:33
  • What's the difference between resetting the screen when you leave and resetting the screen when you arrive? – remeus Aug 01 '19 at 07:23
  • I load data from api on one of the screen of the menu of drawer Navigator. I just wanted to fetch the data again when menu on the drawer is pressed. – Kamalesh kunwar Aug 01 '19 at 09:27

1 Answers1

0

If you want to close all screens and go back to the beginning, use the popToTop'function.

The popToTop action takes you back to the first screen in the stack, dismissing all the others. It's functionally identical to StackActions.pop({n: currentIndex}).

Usage

import { StackActions } from 'react-navigation';

this.props.navigation.dispatch(StackActions.popToTop());
hong developer
  • 13,291
  • 4
  • 38
  • 68
  • This does not helped me. This rest the stack of screens. I wanted to reset the menu's screen of drawer navigator which has been pressed. It just resumes now. How can it be reset ? – Kamalesh kunwar Aug 01 '19 at 06:33
  • Code cannot be posted here. `Select an answer`, `complete the questions`, and `post the new questions`. `Reset` can be done by using the reset used in the question. – hong developer Aug 01 '19 at 13:49