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
Support <Redirect> in <Switch> #4541
Comments
An interesting idea. However, I'm not sure it would have the intended consequence. To me, it looks like the Redirect is either misplaced or will always be performed, regardless of the Switch's match. The Route with a render prop is more verbose, but definitely clearer about what's going to happen here. |
So, to me, it's harder to see at a glance that the last |
You could do this if the undefined path is not clear: const createRedirect = to => () => <Redirect to={to} />
// ...
<Route path="/*" component={createRedirect('/foo')} /> |
A bit sugary to my taste, with runtime cycles and memory going to devel time enjoyment… |
I like it. Maybe in this case <Switch>
<Route exact path="/home" .../>
// Simple <Redirect>
<Route path="/about" .../>
<Redirect from="/company" to="/about"/>
// <Redirect> with dynamic URL segments
<Route path="/users/:userId" .../>
<Redirect from="/profile/:userId" to="/users/:userId"/>
// A <Redirect> without a "from" prop always matches. Same as a <Route> with no "path".
<Redirect to="/home"/>
</Switch> |
* `<Redirect>` already worked because it doesn't have a `path` and it gets rendered :) * Add `from` as an alias for `path` in `matchPath` so you can write `<Redirect from="/old" to="/new"/>` Fixes remix-run#4541
@mjackson turns out that was remarkably easy to do - just make |
* `<Redirect>` already worked because it doesn't have a `path` and it gets rendered :) * Add `from` as an alias for `path` in `matchPath` so you can write `<Redirect from="/old" to="/new"/>` Fixes #4541
@wmertens Feels like validation that we're on the right track! :D |
Definitely, I love the new API! I was pretty annoyed with the previous RR versions, but now, I'm a fan! 💯 (especially with the upcoming relative path support, leading to true componentization) |
What do you think of:
PS: What's the use-case for redirect with push? |
@hamzakubba |
@pshrmn I know, I meant why would anyone want to do that? I was curious why it was implemented on the |
#3903 appears to be the origin of that, so I assume that there are use cases listed there (just skimmed, didn't really look that close). |
@hamzakubba I like the |
Is this supported now? I thought it was but couldn't get it to work - went with |
Right now, to implement a redirect fallback in a switch, you need to do
How about checking for the
to
parameter in the Switch loop and matching directly:The text was updated successfully, but these errors were encountered: