Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Navigation - navigating to another tab and reset stack

I'm trying to route from one StackNavigator to another, both of which are inside a TabNavigator. I'm currently able to get there by simply doing:

this.props.navigation.navigate('Screen3') 

But I also want to reset that tab when I go to it. Here is how my app navigators are generally set up:

- Main (StackNavigator)   - LoginScreen   - MainTabs (TabNavigator)     - Tab1 (StackNavigator)       - Screen1       - Screen2     - Tab2 (StackNavigator)       - Screen3       - Screen4 

How could I navigate to Screen3 but also reset the StackNavigator Tab2?

I've also tried doing this, to no avail:

let resetAction = NavigationActions.reset({   index: 0,   key: 'Tab2',   actions: [     NavigationActions.navigate({ routeName: 'Screen3' })   ], });  this.props.navigation.dispatch(resetAction); 
like image 633
AHinson Avatar asked Jun 01 '17 16:06

AHinson


People also ask

How do I reset the navigation stack in react navigation?

To reset the navigation stack for the home screen with React Navigation and React Native, we can use the navigation. dispatch and the CommonActions. reset methods. import { CommonActions } from "@react-navigation/native"; navigation.

How do I reset my tab navigation?

first time screen init TabA ,user click TabB >> TabB has button click to PageA >> PageB >> PageC ,PageC has a button to reset to Tab. let resetAction = NavigationActions. reset({ index: 0, actions: [ NavigationActions. init({routeName: 'Tab'}), ] }); this.

Can you have multiple stack navigators?

Since you have multiple screens in common and some different data (title, description, etc), you can create only one common stack navigator with multiple screens, and then you can call it from anywhere else.

What does the Replace method of Stackaction do?

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. params - object - Params to pass to the destination route.


1 Answers

You'll have to dispatch two navigation actions, one to reset the stack of the current tab and another to navigate to the next screen:

let resetAction = StackActions.reset({   index: 0,   actions: [     NavigationActions.navigate({ routeName: 'Screen1' })   ], });  this.props.navigation.dispatch(resetAction); this.props.navigation.navigate('Screen3'); 

Here is a snack

like image 181
bitman Avatar answered Sep 27 '22 20:09

bitman