Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to go back to previous visited route programmatically? #61

Closed
yofret opened this issue Jun 27, 2017 · 3 comments
Closed

How to go back to previous visited route programmatically? #61

yofret opened this issue Jun 27, 2017 · 3 comments

Comments

@yofret
Copy link

yofret commented Jun 27, 2017

I have reading the Docs on how to go back to previous State/page visited, but I don't find anything in the docs, please if you could point me the right direction that would be great

Thanks! and awesome work!

@elboman
Copy link
Member

elboman commented Jul 1, 2017

The best thing would be to get the previous state from the last transition and use that as target:

Inside a routed component you can access the transition via props:

  render () {
    let {transition} = this.props;

    // access the state we navigated from
    let prevState = transition.from();
    let prevParams = transition.params('from');

    // navigate to the state
    transition.router.stateService.go(prevState, prevParams);
  }

Since the router is synced with the browser history you can always navigate back using the DOM api:

window.history.back();

I guess it depends on what you need to achieve.

The first method is useful for handling redirects inside the application. In the sample app we redirect users that are not logged in to the log in page. Once the user has logged we find out which state triggered the original redirect and redirect the user back to that state (you can see here what I'm trying to explain).

Glad to help! Let me know if you have any more doubts!

@elboman elboman closed this as completed Jul 1, 2017
@MarcoPortillo
Copy link

@elboman Hi, thanks for the explanation.

It didn't work for me, nothing happens if I apply this. Using the DOM api is not an option for me, aren't there any other ways to go back? The documentation says nothing about it.

@yofret
Copy link
Author

yofret commented Sep 20, 2019

Oh Wow, I never replied to this thread, It did work for me when I was using it! I never gave a proper thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants