2.0.1
http://jsbin.com/pitabavaba/1/edit?js,console,output
onEnter should be called again since onLeave was called.
After replace() is called, going back to the dashboard results in no "loading data" message.
Calling onLeave implies that the page has been left. When replace is called, we should call onEnter to keep the flow consistent.
@taion Is this not a bug?
No.
Oops, finger slipped – it's actually just how redirects work in React Router, I think, unless I'm misunderstanding something.
I see – okay, I'm going to update the issue to be more clear on what's happening.
@taion Thanks. I managed to work around the problem myself using an IndexRedirect. Redirect could have also been used if I was redirecting from something other than the index route.
I still think this is a bug though because it doesn't respect the onEnter/onLeave flow someone might expect.
Let me see if I can explain my reasoning a bit better.
Let's give routes two states: active and inactive. By calling onEnter and onLeave we enter either of those states. onEnter enters active and onLeave enters inactive.
While writing these a couple more of your comments came through. Hopefully this clears up anything that wasn't clear before.
Right, here's the simplest case:
<Route>
<Route path="/foo" onEnter={onEnter} onLeave={onLeave} />
<Redirect from="bar" to="/foo" />
</Route>
If the user navigates from /foo to /bar:
/foo to /baronLeave on /fooonEnter on /bar, which transitions us back to /foo/foo/foo to /fooonEnter on /foo (nor onLeave on /bar)The specific confusing behavior is that we call onLeave but not onEnter on /foo.
Related: https://github.com/reactjs/react-router/issues/3148
I believe the correct fix here might be to track some sort of pendingState in the transition manager, then calculate entering and leaving routes relative to the pending state. Not totally sure there – there are a few holes with that logic as well.
For example, suppose the target state has routes A, B, C, D, E. Suppose route B triggers a redirect in the onEnter. If this happens, we don't call onEnter on C, D, or E, so we probably shouldn't call onLeave on those either, but that would require yet additional bookkeeping.
We also can't just "rewind" the onLeave hooks, because they might trigger a redirect. Possibly we need to track some sort of "pending left routes", and e.g. intersect those in some appropriate way.
This seems messy enough that I don't see a good way to fix this in the near term. As always, we recommend not doing this sort of data fetching in onEnter hooks.
Hi,
I have the same issue, but it is related to the auth flow.
I have a login route and an index.
Index has onEnter set to a function that check if the user is logged in, otherwise it replace state with '/login'.
Login page has onEnter that redirect to '/' if the user is logged in yet.
Now, if I logged In and the go back with browser back button, it goes on login page and doesn't call onEnter, so I'm no more able to check authentication. The same if I logout and go back to index page.
Can we found some way to solve the issue?
Thanks!
You appear to be hitting something different, most likely a usage error on your part. Please consult one of the support forums.
Man you're right!!
It's my fault, but was a really similar bug that i have...
I have a middleware per hook system. It was bugged!
Sorry for mistakes!
Most helpful comment
Right, here's the simplest case:
If the user navigates from
/footo/bar:/footo/baronLeaveon/fooonEnteron/bar, which transitions us back to/foo/foo/footo/fooonEnteron/foo(noronLeaveon/bar)The specific confusing behavior is that we call
onLeavebut notonEnteron/foo.