0

Is it possible to use optional parameter for url? For example path fruits/:id will match the same component if parameter id is not passed;

<Route path="/fruits/:id">
  <Fruits />
</Route>

At the moment to implement it I need to declare two routes fruits:/id and /fruites/ to match the same component.

<Route path="/fruits/:id">
  <Fruits />
</Route>

<Route path="/fruits/">
  <Fruits />
</Route>

2 Answers2

0

Just define route like this

<Route path="/fruits/:id?">

this will work for both /fruits and /fruits/1

Anku Singh
  • 914
  • 6
  • 12
0

You're using react-router so you don't have to worry about it. React router will match both /fruits and /fruits/1 using this code.

<Route path="/fruits/:id">
  <Fruits />
</Route>