'Redirect with matched parameters with react-router-dom doesn't work

I'm using react-router-dom:4.3.0-rc.3 .
The route component

<Switch>
  <Redirect from='/p/:userId' to='/p/:userId/main' />
  <Route path="/p/:userId/main" component={Main} />
</Switch>

when I get a url /p/123456 it redirect to /p/:userId/main and lost userId .
I am confused about this.In official site, i can't get the answer.



Solution 1:[1]

A <Redirect> doesn't do any pattern compiling. You have to give it the actual URI that you want to redirect to.

The approach that I would take would be to use a <Route> instead of a <Redirect> for the matching, and then use the parsed param to build the redirect URI.

<Route path="/p/:userId" render={({ match }) => (
  <Redirect to={`${match.url}/main`} />
)} />

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anil Kumar