question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for redirect/to property in routes definition for react-router-config

See original GitHub issue

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:

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:closed
  • Created 6 years ago
  • Reactions:45
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

17reactions
jharris4commented, Jun 28, 2017

@gl0gl0 Good question about redirecting with params.

I think it should be as simple as something like this:

{ path: '/:id', exact: true, component: (props) => <Redirect to={"/pathA/" + props.match.params.id } /> }

Give that a try and let me know if it works.

10reactions
UlyssesInvictuscommented, Jan 9, 2019

Minor bug that I ran into while trying to implement this: even though you’re using a redirect, you still have to provide exact: true if 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 exact param, it still redirects, but seems to get caught up on the previous match so that a “new” match isn’t applied. (And any calls to renderRoutes seem 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found