Reset reaction-navigation Stack from the box

I was looking for a solution for 2 days.

A lot of questions open on the official react-navigation github: https://github.com/react-community/react-navigation/issues , but no one provides a real solution, including one post, hey guys, look at this problem , many problems ": https://github.com/react-community/react-navigation/issues/691 unanswered.

Basically: I have a DrawerNavigator with some nested StackNavigator .

 export const MainNavigator = DrawerNavigator( { OrderNew: { screen: OrderNew, navigationOptions: { drawerLabel: DrawerLabel('New Order') } }, Orders: { screen: OrderNavigator, //a StackNavigator navigationOptions: { drawerLabel: DrawerLabel('Orders') } }, MenuNavigator: { screen: MenuNavigator, //a StackNavigator navigationOptions: { drawerLabel: DrawerLabel('Menu') } }, //more screens... { initialRouteName: 'Orders', drawerPosition: 'right', contentComponent: props => <ScrollView style={{backgroundColor: 'rgba(227, 100, 29, .95)'}}><DrawerMenu state={props} /></ScrollView>, } ); 

So, I pass the contentComponent my menu component. My idea is to overwrite the onItemPress event of the onItemPress component and put my logic here.

I want: I need to reset MenuNavigator (and other stack screens) every time users click this item. I just can't get it. As I said, I tried a lot of code. In fact, it is like this:

 onItemPress={ router => { const navigateAction = NavigationActions.navigate({ routeName: router.route.routeName, }); this.props.state.navigation.dispatch(navigateAction); } } 

The above code works, I can navigate through the elements of the box. Now, in my head, I just need to pass the subtitle by reloading the first element of the navigator. So I tried:

 onItemPress={ router => { const navigateAction = NavigationActions.navigate({ routeName: router.route.routeName, action: NavigationActions.reset({ index: 0, actions: [ NavigationActions.navigate({routeName: router.route.routes[0].routeName}), ] }) }); this.props.state.navigation.dispatch(navigateAction); } } 

And the error will explode:

There is no route for the key menu. Must be one of: "Orders", "Customer"

To StackNavigator : how could I reset StackNavigator when I switch to another DrawerNavigator element?

+5
source share

Source: https://habr.com/ru/post/1270596/


All Articles