Sometimes, we want to set the default route to another Route in React Router.
In this article, we’ll look at how to set the default route to another Route in React Router.
How to set the default route to another Route in React Router?
To set the default route to another Route in React Router, we can use the Navigate
component.
For instance, we write
<Routes>
<Route path="/" element={<Navigate to="/searchDashboard" />}>
<Route path="searchDashboard" element={<SearchDashboard />} />
<Route path="*" element={<Navigate to="/" />} />
</Route>
</Routes>
to set the route for path /
to navigate to the /searchDashboard
route.
Likewise, we have the *
catch all route that does the same thing.
And /searchDashboard
renders the SearchDashboard
component.
Conclusion
To set the default route to another Route in React Router, we can use the Navigate
component.