Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resetting the navigation stack for the home screen (React Navigation and React Native)

I've got a problem with the navigation of React Navigation and React Native. It is about resetting navigation and returning to the home screen.

I've build a StackNavigator inside of a DrawerNavigator, and the navigation between home screen and other screens is working. But the problem is, that the navigation stack grows and grows. I'm not sure how to remove a screen from the stack.

For example when going from the home screen to the settings screen, then to the entry screen and lastly again to the home screen, the home screen is twice in the stack. With the back button I do not get out of the app, but again to the entry screen.

When selecting the home button again a reset of the stack would be great, but I don't know how to do this. Here someone tried to help an other person with a similar problem, but the solution didn't work for me.

const Stack = StackNavigator({   Home: {     screen: Home   },   Entry: {     screen: Entry   },   Settings: {     screen: Settings   } })  export const Drawer = DrawerNavigator({   Home: {     screen: Stack   }},   {     contentComponent: HamburgerMenu   } ) 

And this is a simple example of the drawer screen

export default class HamburgerMenu extends Component {   render () {     return <ScrollView>       <Icon.Button         name={'home'}         borderRadius={0}         size={25}         onPress={() => { this.props.navigation.navigate('Home')}}>         <Text>{I18n.t('home')}</Text>       </Icon.Button>        <Icon.Button         name={'settings'}         borderRadius={0}         size={25}         onPress={() => { this.props.navigation.navigate('Settings')}}>         <Text>{I18n.t('settings')}</Text>       </Icon.Button>        <Icon.Button         name={'entry'}         borderRadius={0}         size={25}         onPress={() => { this.props.navigation.navigate('Entry')}}>         <Text>{I18n.t('entry')}</Text>       </Icon.Button>     </ScrollView>   } } 

I hope you can help me. This is an essential part of the navigation and a solution would be great!

like image 551
Daniel Avatar asked Mar 29 '17 10:03

Daniel


People also ask

What does the reset method of stack action do?

The reset action wipes the whole navigation state and replaces it with the result of several actions. index - number - required - Index of the active route on routes array in navigation state . actions - array - required - Array of Navigation Actions that will replace the navigation state.


2 Answers

React Navigation 5.x , 6.x

import { CommonActions } from '@react-navigation/native';  navigation.dispatch(   CommonActions.reset({     index: 1,     routes: [       { name: 'Home' },       {         name: 'Profile',         params: { user: 'jane' },       },     ],   }) ); 

Available in Snack

like image 177
Mahdi Bashirpour Avatar answered Sep 21 '22 21:09

Mahdi Bashirpour


This is How I do it :

reset(){     return this.props                .navigation                .dispatch(NavigationActions.reset(                  {                     index: 0,                     actions: [                       NavigationActions.navigate({ routeName: 'Menu'})                     ]                   }));   } 

at least replace 'Menu' with 'Home'. You may also want to adapt this.props.navigation to your implementation.

In version > 2 follow this:

import { NavigationActions, StackActions } from 'react-navigation';         const resetAction = StackActions.reset({                 index: 0,                 actions: [NavigationActions.navigate({ routeName: 'MainActivity' })],             });  this.props.navigation.dispatch(resetAction);  
like image 45
Robin Dehu Avatar answered Sep 22 '22 21:09

Robin Dehu