Backwards navigation helpers
suchipi opened this issue · 5 comments
Per https://twitter.com/sindresorhus/status/953743983678717955
We have these two helpers in our app which have been useful:
goBackTo(pathname)
Looks back in history to find which entry matches pathname, and then moves back to that entry. We implemented using MemoryRouter; depending on the limitations of BrowserRouter, it might but always be possible (I don't think BrowserRouter has an entries array on router.history). This is useful when you have routes in history that the user should not go back to, for example when going through a wizard or filling out a multi-step form.
clearHistoryAndPush(location)
Goes back history.length - 1 and then replaces the current route with the new location (where location is anything that can be passed to history.push). This can be useful when, for example, the user logs out of the app.
There is a $30.00 open bounty on this issue. Add more on Issuehunt.
- Checkout the Issuehunt explorer to discover more funded issues.
- Need some help from other developers? Add your repositories on Issuehunt to raise funds.
Also, with goBackTo
, if it can't find the pathname in history, it throws an Error.
@suchipi Thanks for elaborating. These methods sound useful.
@IssueHunt has funded $30.00 to this issue.
- Submit pull request via IssueHunt to receive this reward.
- Want to contribute? Chip in to this issue via IssueHunt.
- Checkout the IssueHunt Issue Explorer to see more funded issues.
- Need help from developers? Add your repository on IssueHunt to raise funds.
I would like to work on this. The above 2 methods are MemoryRouter
friendly, especially the goBackTo
method. The only way I see to search through previous history entries is by maintaining our own history.entries
even when createBrowserHistory
is used.
The 2nd method can still be achieved by using history.go()
and then pushing the argument location.
What should it be?
I don't know what the best solution would be as I have not had time to look into it. Instead of asking questions, it's usually better to propose recommendations.