Oblomov
Oblomov

Reputation: 9625

Using react router to route several paths to the same component

I got the following routes in my app:

<div>
   <Route exact path='/:category' render={() => (
      <Posts/>
   )}/>
   <Route exact path='/' render={() => (
      <Posts/>
   )}/>
</div>

Where the second route is just an extension of the first and routes to the same component. How can I combine both routes in one Route tag?

Upvotes: 2

Views: 1194

Answers (1)

MEnf
MEnf

Reputation: 1512

You can use <Route exact path='/:category?' component={Posts} /> what the question mark does is make the url extension an optional value.

Upvotes: 2

Related Questions