react-router-util icon indicating copy to clipboard operation
react-router-util copied to clipboard

Backwards navigation helpers

Open suchipi opened this issue 7 years ago • 5 comments

Issuehunt badges

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.

suchipi avatar Jan 18 '18 03:01 suchipi

Also, with goBackTo, if it can't find the pathname in history, it throws an Error.

suchipi avatar Jan 18 '18 04:01 suchipi

@suchipi Thanks for elaborating. These methods sound useful.

sindresorhus avatar Jan 21 '18 20:01 sindresorhus

@issuehunt has funded $30.00 to this issue.


IssueHuntBot avatar Mar 21 '19 08:03 IssueHuntBot

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?

danedavid avatar Jul 13 '19 17:07 danedavid

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.

sindresorhus avatar Jul 25 '19 22:07 sindresorhus