Skip to content
This repository was archived by the owner on Oct 26, 2018. It is now read-only.
This repository was archived by the owner on Oct 26, 2018. It is now read-only.

Redirect not preserving params #596

@amaschas

Description

@amaschas

When I attempt to use redirect like this:

<Switch>
  <Route path="/foo/:id/landing" component={FooComponent} />
  <Redirect from="/foo/:id" to="/foo/:id/landing" />
</Switch>

Visiting foo/2 redirects me to foo/:id/landing. The preferred behavior would be to interpolate the param and redirect to foo/2/landing.

I'm using a workaround from this thread for now, but this should probably be the default behavior.

import pathToRegexp from 'path-to-regexp';
import { Route, Switch, Redirect } from 'react-router-dom';

const RedirectWithParams = ({ exact, from, push, to }) => {
   const pathTo = pathToRegexp.compile(to);
   return (
     <Route exact={exact} path={from} component={({ match: { params } }) => (
       <Redirect to={pathTo(params)} push={push} />
     )} />
   );
 };

 export default RedirectWithParams;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions