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

top() to get the previous route #426

Closed
ChrisCinelli opened this issue Feb 7, 2017 · 16 comments
Closed

top() to get the previous route #426

ChrisCinelli opened this issue Feb 7, 2017 · 16 comments

Comments

@ChrisCinelli
Copy link

There are use cases (see for example the gmail app on android), where the navigation appearance depends from where you are coming from.
Considering back button and other behaviors, it would be great to have a .top() that returns the route where you go when you click the back button.

@ChrisCinelli ChrisCinelli changed the title top() to get the previus route top() to get the previous route Feb 7, 2017
@mjackson
Copy link
Member

mjackson commented Feb 7, 2017

This is more of a routing issue than a history issue, but you should be able to just use goBack to mimic Android's back button, like we do in react-router-native's <AndroidBackButton> component. I've been meaning to dig in further and create a more comprehensive demo, but haven't had time yet. Please try it out and let us know how it goes.

@mjackson mjackson closed this as completed Feb 7, 2017
@ChrisCinelli
Copy link
Author

ChrisCinelli commented Feb 10, 2017

@mjackson: I do not want to trigger a "go back", I want to be able to access what is in the state of the previous route from a stack prospective.

That would enable us to render a " <-" in the navigation bar when the user is coming from a specific route or a default navigation when the user is coming from other routes.

@mjackson
Copy link
Member

We currently render back/forward buttons on our website using memory history's canGo method. It sounds like that would solve your use case.

@ChrisCinelli
Copy link
Author

canGo can solve an edge case if the user gets on the site directly but it does not fix routes inside the product that need to show different navigations BASED ON WHERE YOU ARE COMING FROM.

Think for example if instead of a "<-" you want to have a label "back to messages" or "back to the homepage"... you cannot do that with canGo.

The full solution consist in being able to inspect the top of the stack (where you are coming from). The back URL are a stack and it is pretty common for a stack to have a top() method.

Currently as a workaround, I have to push extra info in the state as I move to a new route (ex: {from: "messages"}. This is not ideal though.

@mjackson
Copy link
Member

Since you're using memory history, just inspect history.entries. That's a list of all the entries in the stack. Be aware though that this will break on browser/hash history where we don't have the ability to see where we came from.

@damianobarbati
Copy link

Any news on this? Indeed being able to access previous route would be nice.

My use case: this would make trivial redirecting a user once logged on the page he came from

protected page => auth => protected page again

@mjackson
Copy link
Member

You can already do this. There is nothing for us to add.

@damianobarbati
Copy link

@mjackson what do you mean? where can I find the previous route with History without keeping track of it myself?

@mjackson
Copy link
Member

Inspect history.entries and history.index. It's all there. history.entries[history.index - 1] is the last location.

@damianobarbati
Copy link

@mjackson thanks for helping! Maybe this is something related react-router-redux? Honestly docs and discussion around the web are very fragmented and I can't understand if this is related to history or react-router-redux module :(

import { browserHistory } from 'react-router';
import { syncHistoryWithStore } from 'react-router-redux';

export const history = syncHistoryWithStore(browserHistory, store);
console.log(history); //no entries property!

@SkyHustle
Copy link

@mjackson Can you please explain where entries is coming from? The browser'sHistory object doesn't have that method? I to need a way to get the path/route/location from which the user came from. I'm using React Router, not using Redux.

@mjackson
Copy link
Member

@Dmitry1007 history.entries is only available when using memory history, not browser history.

@damianobarbati
Copy link

@mjackson can you please show us what is the proper way to get the previous route when using browser history then?

@mjackson
Copy link
Member

@damianobarbati You can't. Browsers won't let you because it's a security issue. Just imagine if a website could read all the URLs that you had visited previous to coming to that site. It would be a violation of that user's privacy.

@damianobarbati
Copy link

@mjackson oh no I totally agree, but I didn't mean that! I meant how to read the same domain/website previous url without having to keep track of it xD

Imagine this scenario:

  • user gets on protected url
  • user is automatically redirected to login box (router logic)
  • user is automatically redirected to protected url (here comes history to play!)

@mjackson
Copy link
Member

I meant how to read the same domain/website previous url without having to keep track of it

We don't provide API for that in browsers because we don't have a reliable way to persist the data across page refreshes. You'll have to do it yourself.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants