-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Added Redirect with parameters #5209
Added Redirect with parameters #5209
Conversation
*/ | ||
const generatePath = (pattern = '/', params = {}) => { | ||
if (pattern === '/') { | ||
return pattern |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Obviously, this pattern does not contain any parameters. For an additional performance shortcut, we could assume that all strings that do not contain *
, (
, )
or :
are eligible to pass through before creating and caching a compile function.
Is there anything which keeps this PR from being merged? |
I was actually just about to file a request for this feature. Seems like it's been waiting a while for a merge. |
Just out of curiosity, do you see any solutions for e.g. |
Thank you @dlindenkreuz for implementing this! I've been wanting to tackle this for a while now. This will definitely make it into our next release 😅 |
@mjackson Sweeeet 🎉 @jochenberger More details:
You might want to wrap the redirect in a const dynamicRedirect = ({ match }) =>
<Redirect to={{pathname: "/users", search: `?id=${match.params.userId}`}} />
// ...
<Route path="/users/:userId" render={dynamicRedirect} /> |
Will there be a release anytime soon that includes this change? Thx. |
This implementation does not account for paths with param modifiers. For example: <Redirect from="/about/:rest+" to="/:rest+" />
// expected: /about/foo/bar → /foo/bar
// actual: /about/foo/bar → /foo%2Fbar I did a quick fix in my own project by just appending |
Its not in the currently latest release of |
@barrystaes There is. Or look up the tags. |
Thanks @timdorr! Not sure on how to find it (by commit) in Github however.. like |
👋 bumping this since I would also love this change. are there any plans for a release? |
Yes, it will be released. |
@timdorr do you have ETA? |
Nope. |
Looking forward to the release. |
Also looking forward to this release! |
This pull request adds back a feature from v3 where the
to
URL pathnames of<Redirect>
could contain parameters.Currently, parameters in
from
are already matched by<Switch>
and then passed into<Redirect>
ascomputedMatch
prop just like<Route>
children. However,<Redirect>
does not do anything with that information yet.I implemented a new
generatePath(pattern, params)
function that creates a new pathname using path-to-regexp'scompile
function. This guarantees that patterns are processed in the same way as they are parsed inmatchPath
. This function is used to put the params fromcomputedMatch
into the respective slots of theto
prop, which may from now include named parameters as well.All
compile
functions are cached bypattern
using the same mechanism asmatchPath
.Most importantly, the matching logic remains completely untouched and as-is. All of this is meant to be minimally invasive.
Tests, docs and batteries included. It might make sense to make
generatePath
available as a public API helper method and document it likematchPath
.Examples (without
<Switch>
for brevity)