Andy Fleming
Andy Fleming

Reputation: 7865

Route parameter not included in redirect

Premise

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.


Question(s)

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?


Other notes

Upvotes: 1

Views: 460

Answers (1)

JustinTRoss
JustinTRoss

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

Related Questions