Badrush
Badrush

Reputation: 1287

Reuse params in React Router <Redirect />

I want to redirect from /web/ or /beta/ to /web/dashboard or /beta/dashboard/, respectively.

I tried using as follows ...

<Redirect exact from="/:platform(web|beta)" to="/:platform/dashboard" />

The problem is that when I got to www.abc.com/beta it simply redirects me to www.abc.com/undefined/dashboard instead of replacing the :platform with beta.

For Reference I'm using the following version: "react-router-dom": "^4.2.2"

Upvotes: 0

Views: 95

Answers (2)

Badrush
Badrush

Reputation: 1287

One solution that I found works is instead of relying on the params, you can access the location.pathname prop.

<Redirect exact from="/:platform(web|beta)/" to={`${this.props.location.pathname}/dashboard`}/>

Upvotes: 1

ztadic91
ztadic91

Reputation: 2804

you can get those params from the props

for the latest version of react router it's

this.props.match.params.platform

Docs

Upvotes: 0

Related Questions