Say I've navigated through 4 screens in my StackNavigator App and now I want to go back to the first screen. There seems to be three different ways to do this and they do navigate to where I want to do, however each way has an animation that cycles through each previous screen.
Is there a clean way to navigate from SCREEN_D
to SCREEN_A
?
In other words, I don't want to see SCREEN_C
and SCREEN_B
a split of a second before seeing SCREEN_A
when navigating backwards from SCREEN_D
navigation.navigate(SCREEN_A); ... navigation.navigate(SCREEN_B); ... navigation.navigate(SCREEN_C); ... navigation.navigate(SCREEN_D);
Three ways to do this:
1.
return this.props.navigation .dispatch(NavigationActions.reset( { index: 0, actions: [NavigationActions.navigate({ routeName: 'SCREEN_A'})] }));
2.
const {SCREEN_B_KEY} = this.props.navigation.state.params this.props.navigation.goBack(SCREEN_B_KEY)
3.
const defaultGetStateForAction = Navigation.router.getStateForAction; Navigation.router.getStateForAction = (action, state) =>{ if(action.type === "Navigation/BACK"){ const routes = [ {routeName: 'First'}, ]; return { ...state, routes, index: 0, }; } return defaultGetStateForAction (action,state); }
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.
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.
Use the goBack() Method to Go Back One Screen in React Native. The goBack() method is one of the most important methods in the react-navigation library. It allows you to go back to one of the previous screens in your navigation stack.
react-navigation has popToTop
and we can use it like blow
this.props.navigation.popToTop()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With