Closed
Description
I don't fully understand what this section of the documentation means:
Optionally provide a key, which specifies the route to go back from. By default, goBack will close the route that it is called from. If the goal is to go back anywhere, without specifying what is getting closed, call .goBack(null);
Here's what I am trying to do, I have the stack like this:
- home
- credit-card
Notice that I am using REDUX! From inside the credit-card
scene I am doing the following hoping it will "pop" both credit-card
and email
scenes to finally show the home
screen:
navigation.goBack('home');
This is doing nothing, and the stack is not being altered in any way. How can I accomplish this? (I volunteer to enhance the documentation once I figure out how it works).
Activity
machadogj commentedon Feb 10, 2017
I found the problem, the stack router is comparing by
key
, when according to the state I have in my app, it should be comparing byrouteName
:https://github.com/react-community/react-navigation/blob/master/src/routers/StackRouter.js#L207
machadogj commentedon Feb 10, 2017
When I changed the following to compare using
routeName
:const backRoute = state.routes.find(route => route.routeName === action.key);
I can tell now that the functionality is poping all the scenes including the
routeName
one. I believe that we could really use agoBackTo
that leaves therouteName
in the stack (backRouteIndex - 1
).aimak commentedon Mar 16, 2017
I had the same understanding of what the
goBack('Home')
should have done and I struggle with the same issue.However, I don't understand why this issue was closed. Is this fixed ? Is there a PR for this ?
edo1493 commentedon Mar 25, 2017
This is not clear to me too. I actually don't understand how to implement something like popN() or popToTop() from Navigator/NavigatorIOS.
aimak commentedon Mar 25, 2017
What I understood from @machadogj's explanation and after a quick look at the code, the "bug" can easily be fixed : right now
goBack(screenKey)
takes the user to the screen before the screen with 'screenKey' as key. But it would be nice to give the screen name as parameter.But I'm still not sure if the fix is something wanted by the Contributors or not. I tried to ping @machadogj several times but never succeeded :(
edo1493 commentedon Mar 25, 2017
I kinda understand that logic, but I was also wondering how to retrieve the screenkey of the route I want to go back to. I tried to look into the chrome debugger and it seems they are all randomly generated at runtime.
edo1493 commentedon Mar 25, 2017
I mean if I can do it by key, no biggie. Do you know how to retrieve the key of each screen/route? @aimak
sunnylqm commentedon Mar 28, 2017
'key -> routeName' seems to be a forgotten change.
ping @skevy @grabbou @satya164 @ericvicenti
P.S. This repo moves quite slowly, it's a hurry to make it a replacement for Navigator (and even you guys deprecated Navigator before 0.43 release!)
j-mendez commentedon Mar 28, 2017
janiokq commentedon Mar 28, 2017
I try to modify the code effect is reached But the return of animation Seems a little bit not normal
if (action.type === NavigationActions.BACK) {
I call
global.navigatorApp._navigation.goBack(global.navigatorApp.state.nav.routes[1].key);
I have 3 pages 1 2 3 I return from 3 to 1 But he carried out two times return to animation 3 to 2 to 1
machadogj commentedon Mar 28, 2017
According to the roadmap this will not be used in core? roadmap
alimek commentedon Mar 28, 2017
it also doesn't work for me when i try go back by Key. Why is this closed?
mu1ex commentedon Mar 29, 2017
This works. But the api is a bit confusing.
Consider the following scenes on stack
A -> B -> C -> D
if you want to go to A from D you should do
navigation.goBack(keyOfB)
in D.Its sort of confusing and would have been simpler if you could pop to the specified scene id.
38 remaining items
ranran2 commentedon Jul 21, 2017
@guoliang1206 Does your problem is when your reset, the screen between will flash?? if is, now the only way to solve this is to forbid the animation.
ColCh commentedon Jul 21, 2017
@guoliang1206 can you provide minimal launchable example? On https://expo.io/ or something
guoliang1206 commentedon Jul 21, 2017
reducer
and you can see, my Tab is disappear .
ranran2 commentedon Jul 21, 2017
@guoliang1206 i met this flash before, and the best way i found is to forbid the animation when you reset .
peacechen commentedon Jul 21, 2017
@guoliang1206
Be sure to use the route's key (e.g. 'id-xxxxxxxx-#') and not its routeName parameter (in your case ScreenA1). This underscores the need to fix the documentation.
guoliang1206 commentedon Jul 24, 2017
@peacechen The screen in the Tab, key of that is "ScreenName",

And then I push subScreens from this screen which in the tab. Now I need back to this tab screen, should I use what key ?
yaronlevi commentedon Jul 28, 2017
Hope the next release will include something like goBack(2) and goBack('routeName')
nwkeeley commentedon Aug 11, 2017
@guoliang1206 did you find a solution? I tried all the items you've tried
guoliang1206 commentedon Aug 14, 2017
@nwkeeley no, I didn't find a perfect solution, now I only forbid the animation when it pop to rootView
ollyde commentedon Aug 30, 2017
Any solution for this? It's preventing my app from passing quality assurance.
Should be as simple as goBack('nameofscreen') but that produces flashing and doesn't work half of the time.
santhosh77h commentedon Nov 21, 2017
if our scrrens are like A - B - C - D
if we want to go back to A from D we need to store the key of B like this
navigator.navigate('Scrren',{screenKey:navigation.state.key})
so in all the screen we can access
screenKey
usingnavigation.state.params.screenKey
and while we are in the screen D to go back to Anavigation.goBack(navigation.state.params.screenKey)
Now we are in scrrenn A and B - C - D are removed from stack
jaywuYJ commentedon Nov 27, 2017
I tried the solution provided by santhosh77h and it worked, and be aware that we need to pass down the target screen's screenKey as the navigation parameter along the navigation chain so that the last screen in the chain would be able to get the target screen's screenKey.
laizhenhai88 commentedon Dec 28, 2017
the goBack api has no problem, think about this situation:
register need 3 steps from Home, so the stack like this:
Home - p1 - p2 - p3
when we in p3(sometimes the success page), we need close p3, pop p2,p1 and go back to Home, so call goBack(key of p1) in p3 is ok
but, maybe you like call goBack('Home') in p3
if there is another way to start register, such as pay screen, so the stack maybe like this:
Home - some product - pay - p1 - p2 - p3
so, we can't call goBack('Home') in p3, but call goBack(key of p1) in p3 is still ok !
now, the problem is how to store the key of p1, just write your store code in p1
navigator.navigate('Scrren',{screenKey:navigation.state.key})
and call
navigation.goBack(navigation.state.params.screenKey)
in p3because the store code in p1 means we will start a progress (p1-p2-p3), and the key of p1 is the start point, when you want to finish this progress, call goBack(progress start point)
shripada commentedon Jan 12, 2018
To clarify @santhosh77h answer, we need to ensure, we propagate the key of screen 'B' (I am referring to the case @santhosh77h is referring), to screen C, and to Screen D.
ChauVV commentedon Aug 26, 2020
A->B->C->D(Cureent)
just
navigation.navigate('B')