I'm trying to go back two screens. The goal is to go from EditPage
to Cover
. Here is my navigation stack:
Main -> Cover -> EditCover -> EditPage
I read the docs and it says to supply a key of the screen you want to go back from, here's my code:
this.props.navigation.dispatch(NavigationActions.back({key: 'EditCover'}));
I've also tried (with no luck):
this.props.navigation.dispatch(NavigationActions.back('EditCover'));
this.props.navigation.dispatch(NavigationActions.back({key: 'EditCover'}));
this.props.navigation.dispatch(NavigationActions.back({routeName: 'EditCover'}));
this.props.navigation.goBack('EditCover');
this.props.navigation.goBack({key: 'EditCover'});
this.props.navigation.goBack({routeName: 'EditCover'});
The funny thing about all this is that I get no errors. Nothing happens when the code is called...
P.S. If I want to just go back one screen, this code works fine:
this.props.navigation.goBack(null);
P.S.S. In case someone comes across this before there is a solution. This hack works for now:
this.props.navigation.goBack(null);
this.props.navigation.goBack(null);
the
key
property forgoBack()
is a dynamically created string, created byreact-navigation
whenever navigate to a new route.for example:
It is stored in
this.props.navigation.state.key
.So if you want to go from
EditPage
toCover
, what you have to do is to pass key ofEditCover
down toEditPage
, and then callgoBack()
with the key.Why not key of
Cover
butEditCover
?because react-navigation only provide method
goBack(key)
, it's go back from key, not go back to key.EditCover.js
EditPage.js
The right way to do this is with StackNavigation:
According to your question, this is the order of your screens navigation, so when you goBack(null) if you are in
EditPage (goBack) -> EditCover (goBack) -> Cover (goBack) -> Main
You have to call goBack(null) like this in the components:
This is the right way.
For React navigation 2 onward you can use
Also do not forgot to mention in every stacknavigator