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

Smarter goBack

Open simenbrekken opened this issue 12 years ago • 41 comments

Currently one has to use ActiveState with a willTransitionTo hook to know if Navigation.goBack will do anything. It would be nice if goBack returned a boolean that tells you wether it there's anything in history (as managed by react-router).

simenbrekken avatar Oct 15 '14 09:10 simenbrekken

I would also be interested in this. However, I'm not sure what the right solution would be.

Think of a similar use case involving a list view and a detail view. Given a detail view can be visited directly by URL it won't have a meaningful previous history entry (instead it's the empty tab page in Chrome). Therefore, the classic history goBack behaviour will not do anything meaningful when clicked. Perhaps being able to query the managed history would make sense here: If there's no previous entry, do something else in the render method or the click callback.

seidtgeist avatar Oct 15 '14 14:10 seidtgeist

+1! I only use goBack for Cancel buttons but it sucks because I don't know if the user hit page directly.
Unless you use RefreshLocation, it's unlikely you ever want to send user to previous website.

gaearon avatar Nov 17 '14 21:11 gaearon

Seems like the primary use case here is wanting to know whether or not there's anything in the history to goBack to. Would it be enough to just have a canGoBack() method somewhere?

mjackson avatar Nov 23 '14 06:11 mjackson

+1 on canGoBack. I would find that very useful.

rickharrison avatar Nov 23 '14 08:11 rickharrison

:+1:

seidtgeist avatar Nov 23 '14 22:11 seidtgeist

From my understanding canGoBack something router should manage, since browsers don't seem to provide reliable/same API for this.

Should canGoBack be scoped to current application? I think yes. If I came to Google and I'm on my initial route, I suggest it returns false.

Should canGoBack return true or false with RefreshLocation? That's a tricky one, and while returning false is consistent with previous point, it's not too obvious. If canGoBack was called canGoBackWithoutRefreshing it would make more sense, but the name sucks.

Ideas?

gaearon avatar Dec 03 '14 13:12 gaearon

Should canGoBack be scoped to current application? I think yes. If I came to Google and I'm on my initial route, I suggest it returns false.

Definitely. Can't think of a use case where it would make sense to go back to an external website or empty tab. That's why we need this in the first place :)

RefreshLocation

I just read up on RefreshLocation. Tough question. Is it even possible to determine whether the navigation is at the start of history when using it? I couldn't see any state tracking, but I'm not sure it's in there.

seidtgeist avatar Dec 03 '14 14:12 seidtgeist

I think returning false for RefreshLocation is OK. It just says “we can't guarantee you'll stay where you are so proceed on your own risk”.

However there's yet another subtlety here. Should goBack() do anything if canGoBack() returns false?

One real-world example of this happening is when transition.abort() happens on very first route. Currently, it will kick you out of the website. If goBack() is a no-op when canGoBack() is false, it will just render blank screen. What makes more sense? (Should you even be able to abort first transition?)

gaearon avatar Dec 03 '14 15:12 gaearon

Maybe we do this:

  • make goBack a no-op if we don't actually have any history (in the current app, ofc)
  • if we don't have history and the user tries to goBack trigger a warning (so the dev has some feedback about why it didn't work)
  • change the signature of goBack() to goBack(force=false) so devs can force the browser to go back when they're using RefreshLocation. Or maybe just default force=true if we're using RefreshLocation

I think that would satisfy everyone's use cases here.

mjackson avatar Dec 03 '14 15:12 mjackson

Sounds wise. Certainly satisfies mine.

gaearon avatar Dec 03 '14 15:12 gaearon

What would the warning be. In my case if goBack can't accomplish anything, I would instead like to transitionTo another route. So essentially I need this:

if (canGoBack()) {
  goBack();
} else {
  transitionTo();
}

rickharrison avatar Dec 03 '14 19:12 rickharrison

I think goBack should return a boolean of whether action was performed.

if (!goBack()) {
  transitionTo();
}

So it's goBack(force: bool = false): bool.

gaearon avatar Dec 03 '14 20:12 gaearon

@gaearon ya, that sounds good.

mjackson avatar Dec 03 '14 20:12 mjackson

@mjackson Would there also be a canGoBack() so I can determine whether I need to show a back button at all?

seidtgeist avatar Dec 03 '14 21:12 seidtgeist

@ehd I don't think so. Instead, we can expose a length property, similar to History.length so you can do:

if (history.length > 0)
  // show the back button

mjackson avatar Dec 03 '14 21:12 mjackson

@mjackson Sounds good! My specific use case was this:

  1. Path: Go into list, click on detail, needs to show back button
  2. Path: Click on detail link in email, must not show back button

So length would work here.

seidtgeist avatar Dec 03 '14 22:12 seidtgeist

Thanks everyone!

seidtgeist avatar Dec 04 '14 09:12 seidtgeist

Hey, it would be great if the docs could be updated accordingly, they're pretty sparse on goBack() info right now. Was looking for this functionality just now and had to search through the issues to find it.

chrisshiplet avatar Dec 19 '14 21:12 chrisshiplet

Care to create a PR?

Michael Jackson @mjackson

On Fri, Dec 19, 2014 at 1:08 PM, Chris Shiplet [email protected] wrote:

Hey, it would be great if the docs could be updated accordingly, they're pretty sparse on goBack() info right now. Was looking for this functionality just now and had to search through the issues to find it.

— Reply to this email directly or view it on GitHub https://github.com/rackt/react-router/issues/408#issuecomment-67697490.

mjackson avatar Dec 19 '14 21:12 mjackson

Maybe it should be propagated by the Navigation mixin. Currently it does not return anything.

goBack: function () {
    this.context.goBack();
 }

ganmor avatar Feb 19 '15 17:02 ganmor