How to set an optional parameter in the root route in response-router v4?

Let's say I have the following 2 routes:

... <Route exact path="/:param1?/" component={Home}/> <Route path="/news" component={News}/> ... 

now when i try to attack the /news route, the root route for Home launched with param1 parameter ...

I assume that the solution would be to put a question mark in front of param1, like this /?param1 so that it can be ported from routes, but I cannot figure out how to do this in response-router v4

+7
react-router react-router-v4
source share
1 answer

Here is an example of how to do this in white papers. You can find it here .

You will need to use the Switch component with the first / first route.

 <Switch> <Route path="/news" component={News}/> <Route exact path="/:param1?/" component={Home}/> </Switch> 

The switch will always display only one route. Therefore, in the case above, if / news does not become active, then /: param1 will be.

+13
source share

All Articles