Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset the state of a StackNavigator nested in a DrawerNavigatior?

I am building an app whose navigation is based on a DrawerNavigator from the react-navigation library.

This navigator has 3 tabs:

  • 2 regular tabs
  • 1 StackNavigator named "Search"

The StackNavigator consists of one screen that lets the user search for an item, and a second screen where the user sees the search results.

I do not want the search results page to be a tab of a DrawerNavigator, this is why I implemented this structure.

The problem is: if the user has already performed a search, when he clicks on the "Search" tab, he does not come back to the search screen but to the search results screen. I would prefer that the user comes back to the search screen.

How can I achieve that?

like image 571
Arnaud Avatar asked Jun 20 '17 13:06

Arnaud


People also ask

How do you use stack Navigator and drawer navigation together?

Drawer navigator nested inside the initial screen of stack navigator with the initial screen's stack header hidden - The drawer can only be opened from the first screen of the stack. Stack navigators nested inside each screen of drawer navigator - The drawer appears over the header from the stack.

What is the difference between stack Navigator and Tab Navigator?

StackNavigator: When user taps a link, a new screen is put on top of old screens. TabNavigator: User navigates to different screens by tapping on tabs along the top or bottom of the screen.


1 Answers

You can achive this using navigation dispatch with navigationActions

import { NavigationActions } from 'react-navigation';

const resetAction = NavigationActions.reset({
  index: 0,
  actions: [
    NavigationActions.navigate({
      routeName: 'DrawerScreen',
      params: {},
      action: NavigationActions.navigate({ routeName: 'SearchScreen' }),
    }),
  ],
})
navigation.dispatch(resetAction)
like image 59
Tarek Salah uddin Mahmud Avatar answered Oct 23 '22 15:10

Tarek Salah uddin Mahmud