ryouaki
ryouaki

Reputation: 19

react router v4 about how to match two url with one route and one component

Sorry, my english is not good enough.

Version

4.1.1 I want to match two url with one route and one component. just like below:

http://host:port/test/orders/id-1 http://host:port/test/products/id-1 How can I write the path for Route?

Thanks.

Upvotes: 0

Views: 1231

Answers (1)

Shubham Khatri
Shubham Khatri

Reputation: 281606

You can make use of url path parameters to specify multiple paths matching the same route.

In your case the Route will look like

<Route path = "/test/:param/id-1" component={MyComponent}/>

In case you only want to match the /orders/ids-1 and /products/id-1, then you can make use of regex in the path param. react-router makes use of a path that path-to-regexp understands,

The relevant documentation of it is there with the react-router documentation

So you can use

  <Route path = "/test/(orders|products)/id-1" component={MyComponent}/>

Upvotes: 1

Related Questions