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

Behavior discrepancy between Router.replaceWith and transition.redirect #198

Closed
mtscout6 opened this issue Aug 13, 2014 · 1 comment
Closed

Comments

@mtscout6
Copy link
Contributor

I am seeing two different behaviors between three different ways to do the same thing.

  1. Using the Redirect component
    • Behaving as I would expect it so nothing really to address there
  2. Calling Router.replaceWith() in a willTransitionTo call
    • Also behaving as I would expect
  3. Calling transition.redirect in a willTransitionTo call
    • Recalls the willTransitionTo function on every handler in the chain <-- This is the odd duck!

I primarily see this issue when loading the page to a url that will do transition.redirect within a willTransitionTo call. See the console log output on this example: https://github.com/mtscout6/react-router/tree/router-redirect-vs-relaceWith-vs-tansition-redirect/examples/redirect

If you build the example and load the url /abc/mno you will see (as expected):

willTransitionTo called in abc while transitioning to /abc/mno
willTransitionTo called in mno while transitioning to /abc/mno
willTransitionTo called in pqr while transitioning to /abc/mno/pqr 

When you comment out line 36 and uncomment 37 in app.js and rebuild you see this output: (If you don't see this behavior it's because you are not landing on /abc/mno you are transitioning to it)

willTransitionTo called in abc while transitioning to /abc/mno
willTransitionTo called in abc while transitioning to /abc/mno/pqr     <-- This shouldn't have happened
willTransitionTo called in mno while transitioning to /abc/mno/pqr
willTransitionTo called in pqr while transitioning to /abc/mno/pqr 

Note an addition call to willTransitionTo after the transition.redirect in the /abc/mno handler.

@mjackson
Copy link
Member

It doesn't surprise me that Router.replaceWith inside a willTransitionTo is giving you different behavior than transition.redirect. In that case you're essentially circumventing the transition object entirely so it has no idea it has been aborted. You should definitely avoid doing this because you're essentially kicking off a second transition before the first one has completed. In fact, we should probably throw if someone tries to do this. So the first log output you see (the 3 lines) is actually undefined behavior.

The second set of log statements is actually what you should expect. When willTransitionTo is called in abc, it hits the call to transition.redirect and immediately aborts the transition, redirecting to the new route.

Takeaway: for redirects, always use transition.redirect or <Redirect>, which simply calls transition.redirect for you.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 25, 2019
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

2 participants