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);
}
Here's a quick fix. This will remove ALL transitions when navigating (forward or backward).
in transitionConfig add this:
The transitionConfig is a function that returns an object that overrides default screen transitions. https://reactnavigation.org/docs/navigators/stack
This is my working solution for reset back to home (root) without creating new route
categoriesNav
is referenced to my stack navigatorAlso spent some time on this, let me sum up what I discovered, there are multiple solutions/workarounds for this:
1) Use
CardStackStyleInterpolator
The pull request mentioned by Cristiano Santos seems to be merged. So you can load the
CardStackStyleInterpolator
with this import:To apply it like this:
In my case, I just jump to the next screen like:
But in my reducer, I reset the navigator when the
Modal_AddGoodClass
screen is triggered:This works pretty well, except the fact, that still a "back" animation is used instead of a "forward" one.
You can also find here some example code that uses
CardStackStyleInterpolator
.2) Overwrite
getStateForAction
:As Fendrian mentioned here you can overwrite
getStateForAction
of your router to avoid the navigator to go back. This seems to work except for the "swipe back" gesture on iOS:You can do this by using the
popToTop
method from the Navigation prop. Also, if you are using redux, you should update your Redux integration.Hope this helps!