Reputation: 7865
With react-router (v4) it doesn't seem to properly redirect with parameters.
<Redirect from="/people/person/:id" to="/people/:id"/>
If I visit /people/person/123
though, it literally redirects to /people/:id
.
Is this a feature that's missing from the latest version of react router or is my syntax incorrect? Is there another way of accomplishing this?
It is being used within a <Switch>
tag.
I've looked at the syntax here: https://reacttraining.com/react-router/web/api/Redirect
I saw some additional example syntax here: https://github.com/ReactTraining/react-router/issues/1034
Upvotes: 1
Views: 460
Reputation: 787
Your original syntax will not work.
You must grab id from match.params.id see doc example https://reacttraining.com/react-router/web/api/Route:
<Route path="/user/:username" component={User}/>
const User = ({ match }) => {
return <h1>Hello {match.params.username}!</h1>
}
Yours would look like:
<Route path='/people/person/:id' render={props => (
<Redirect to={`/people/${props.match.params.id}`} />
)}/>
Upvotes: 1