Align multiple paths with responsive router v4

Given that I have 2 paths showing the same component, how can I avoid repeating route configurations like:

<Route path="/path1" component={MyComp} />
<Route path="/path2" component={MyComp} />

      

+9


source to share


3 answers


The best solution I've found so far (but seems a little weird):



<Route path="/:path(path1|path2)" component={MyComp} />

      

+15


source


Great answer. It also helps in the following scenario:

<Route path="/path/path1" component={MyComp} />
<Route path="/path/:subpath(path2|path3)" component={MyAnotherComp} />

      



Without (path2|path3)

both components MyComp

and MyAnotherComp

are installed if path /path/path2

.

PS. I would add it as a comment, but I would not have the right :).

+6


source


If you need to allow multiple match (different parameters), but also without a parameter - for example, matching URLs:

  • / questions
  • / questions / posted
  • / questions / answered

This can be done with an optional parameter (note the question mark after the parenthesis)

<Route path="/questions/:tab(posted|answered)?" component={...} />

      

0


source







All Articles