Sometimes, we want to add routes with optional path parameter with React Router.
In this article, we’ll look at how to add routes with optional path parameter with React Router.
How to add routes with optional path parameter with React Router?
To add routes with optional path parameter with React Router. we can add a question mark after the route parameter placeholder.
For instance, we write
<Route path="/to/page/:pathParam1?/:pathParam2?" component={MyPage} />;
to add a Route
component with a question mark after pathParam1
and pathParam2
to add the route parameter placeholder to make them optional.
Conclusion
To add routes with optional path parameter with React Router. we can add a question mark after the route parameter placeholder.