Skip to content

Not sure what .goBack('home') means and how it works #285

Closed
@machadogj

Description

@machadogj

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
  • email
  • 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

machadogj commented on Feb 10, 2017

@machadogj
Author

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 by routeName:

      if (action.type === NavigationActions.BACK) {
        let backRouteIndex = null;
        if (action.key) {
          const backRoute = state.routes.find(route => route.key === action.key);
          backRouteIndex = state.routes.indexOf(backRoute);
        }
        if (backRouteIndex == null) {
          return StateUtils.pop(state);
        }
        if (backRouteIndex > 0) {
          return {
            ...state,
            routes: state.routes.slice(0, backRouteIndex),
            index: backRouteIndex - 1,
          };
        }
      }

https://github.com/react-community/react-navigation/blob/master/src/routers/StackRouter.js#L207

machadogj

machadogj commented on Feb 10, 2017

@machadogj
Author

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 a goBackTo that leaves the routeName in the stack (backRouteIndex - 1).

aimak

aimak commented on Mar 16, 2017

@aimak

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

edo1493 commented on Mar 25, 2017

@edo1493

This is not clear to me too. I actually don't understand how to implement something like popN() or popToTop() from Navigator/NavigatorIOS.

aimak

aimak commented on Mar 25, 2017

@aimak

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

edo1493 commented on Mar 25, 2017

@edo1493

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

edo1493 commented on Mar 25, 2017

@edo1493

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

sunnylqm commented on Mar 28, 2017

@sunnylqm

'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

j-mendez commented on Mar 28, 2017

@j-mendez
janiokq

janiokq commented on Mar 28, 2017

@janiokq

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) {

    let backRouteIndex = null;
    if (action.key) {
      /* $FlowFixMe */
      const backRoute = state.routes.find((route: *) => route.key === action.key);
      /* $FlowFixMe */
      backRouteIndex = state.routes.indexOf(backRoute);
    }
    if (backRouteIndex == null) {
      return StateUtils.pop(state);
    }

    if (state.routes.length >= 2 && backRouteIndex < state.routes.length ) {
      return {
        ...state,
        routes: state.routes.slice(0, backRouteIndex+1),
        index: backRouteIndex,
      };
    }
  }
  return state;
},

I call

global.navigatorApp._navigation.goBack(global.navigatorApp.state.nav.routes[1].key);

94463971-45c2-43c4-9251-f902403b2489

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

machadogj commented on Mar 28, 2017

@machadogj
Author

According to the roadmap this will not be used in core? roadmap

alimek

alimek commented on Mar 28, 2017

@alimek

it also doesn't work for me when i try go back by Key. Why is this closed?

mu1ex

mu1ex commented on Mar 29, 2017

@mu1ex

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

ranran2 commented on Jul 21, 2017

@ranran2

@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

ColCh commented on Jul 21, 2017

@ColCh

@guoliang1206 can you provide minimal launchable example? On https://expo.io/ or something

guoliang1206

guoliang1206 commented on Jul 21, 2017

@guoliang1206

2017-07-21 16_58_39

let navigateAction = NavigationActions.reset({
            index: 0,
            actions: [
                NavigationActions.navigate({ routeName: 'Jobs'})]  
        });
        this.props.navigation.dispatch(navigateAction);

reducer

 case NAVIGATION_RESET:
            // console.log('excute reset navigator ......',action.routeName);
            return AppNavigator.router.getStateForAction(action, state);
            break;

and you can see, my Tab is disappear .

ranran2

ranran2 commented on Jul 21, 2017

@ranran2

@guoliang1206 i met this flash before, and the best way i found is to forbid the animation when you reset .

peacechen

peacechen commented on Jul 21, 2017

@peacechen

@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

guoliang1206 commented on Jul 24, 2017

@guoliang1206

@peacechen The screen in the Tab, key of that is "ScreenName",
2017-07-24 9 07 11
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

yaronlevi commented on Jul 28, 2017

@yaronlevi

Hope the next release will include something like goBack(2) and goBack('routeName')

nwkeeley

nwkeeley commented on Aug 11, 2017

@nwkeeley

@guoliang1206 did you find a solution? I tried all the items you've tried

guoliang1206

guoliang1206 commented on Aug 14, 2017

@guoliang1206

@nwkeeley no, I didn't find a perfect solution, now I only forbid the animation when it pop to rootView

ollyde

ollyde commented on Aug 30, 2017

@ollyde

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

santhosh77h commented on Nov 21, 2017

@santhosh77h

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 using navigation.state.params.screenKeyand while we are in the screen D to go back to A

navigation.goBack(navigation.state.params.screenKey)

Now we are in scrrenn A and B - C - D are removed from stack

jaywuYJ

jaywuYJ commented on Nov 27, 2017

@jaywuYJ

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

laizhenhai88 commented on Dec 28, 2017

@laizhenhai88

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 p3
because 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

shripada commented on Jan 12, 2018

@shripada

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.

//Navigate from B -> C  in B
const key = this.props.navigation.state.key;
this.props.navigation.navigate("C", {screenBKey:key});

//Navigate from C -> D in C  (params now contain a key screenBKey, just pass it down)
this.props.navigation.navigate("D", this.props.navigation.state.params)

//Now to go back from D to A in D.  goBack will need screen B key in order to go to A.
this.props.navigation.goBack(this.props.navigation.state.params.screenBKey);
ChauVV

ChauVV commented on Aug 26, 2020

@ChauVV

A->B->C->D(Cureent)

just navigation.navigate('B')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ColCh@sunnylqm@machadogj@laizhenhai88@aimak

        Issue actions

          Not sure what .goBack('home') means and how it works · Issue #285 · react-navigation/react-navigation