Redirect not preserving params
amaschas opened this issue · 2 comments
amaschas commented
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;
timdorr commented
You have the wrong repo.
amaschas commented
Oops, thanks, will refile over there.