Add support for redirect/to property in routes definition for react-router-config
See original GitHub issueCurrently, if you want to use react-router-config and have a route redirect to another route, you need to do something like the following:
import { Redirect } from 'react-router';
import AppComponent from './AppComponent';
import PathAComponent from './PathAComponent';
import PathBComponent from './PathBComponent';
const routes = [
{
component: AppComponent,
routes: [
{
path: '/',
exact: true,
component: () => <Redirect to="/pathA"/>
},
{
path: '/pathA',
exact: true,
component: PathAComponent
},
{
path: '/pathB',
exact: true,
component: PathBComponent
}
]
}
]
This works fine, but it would be nice to have a simplified shorthand for this case, perhaps something that looks like the following:
const routes = [
{
component: AppComponent,
routes: [
{
path: '/',
exact: true,
redirect: '/pathA'
},
{
path: '/pathA',
exact: true,
component: PathAComponent
},
{
path: '/pathB',
exact: true,
component: PathBComponent
}
]
}
]
This should be fairly easy to support by modifying the implementation of the renderRoutes function to look for a redirect property on routes in addition/instead of the component property.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:45
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Add support for redirect/to property in routes definition for react ...
Currently, if you want to use react-router-config and have a route redirect to another route, you need to do something like the following: ......
Read more >Migrating to React Router v6: A complete guide
Migrate your React Router applications from v5 to v6 with this in-depth guide, including a review of additions and improvements from v5.
Read more >react-router - pass props to handler component - Stack Overflow
Since new release, it's possible to pass props directly via the Route component, without using a Wrapper. For example, by using render prop....
Read more >React router. Route configuration | by Gerardo Fernández
In this way, we will define each route specifying its two properties and we will export an array with all the ones we...
Read more >Upgrading from v5 v6.6.1 - React Router
If you want to redirect client-side, move your <Redirect> into a <Route render> prop. ... For this step, you'll need to install React...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@gl0gl0 Good question about redirecting with params.
I think it should be as simple as something like this:
Give that a try and let me know if it works.
Minor bug that I ran into while trying to implement this: even though you’re using a redirect, you still have to provide
exact: trueif the route you’re redirecting from matches multiple routes (e.g. for the common case of a home page/redirecting to something like/dashboad).If you don’t provide the
exactparam, it still redirects, but seems to get caught up on the previous match so that a “new” match isn’t applied. (And any calls torenderRoutesseem to not execute again.) Hopefully this helps anyone else running into the same thing.Also: this issue is about a year-and-a-half old now–are there any feature implementations for it yet? Any official word that no one’s planning to implement it.