We are using React-Router-DOM v6.19.0 and there are two you can find below.
<Route path="/route1/*" element={<RouteOneList />} />
<Route path="/route1/:one/:two/:three" element={<RouteAnother />} />
and in RouteOneList
it has sub routes
<Route path="/add/:type/:id" element={<AddComponent />} />
Here I am trying to access
"/route1/add/test/1"
– It is supposed to load AddComponent
but it’s falling into <RouteAnother />
component.
Is there any way tackle this problem? I have tried changing the order but that also not helped me.
2
Answers
Make sure to utilize Outlet to render the child routes in
RouteOneList
:When you access
/route1/add/test/1
with this configuration, it should match the more specific route "/route1/add/:type/:id" and render the AddComponent within the RouteOneList.Issue
The issue here is that the
Routes
component only handles matching the routes it renders directly. The"/route1/add/test/1"
path can be matched by the"/route1/:one/:two/:three"
path and so that is what is matched and rendered.Solution
Based on your comment in another answer that
RouteOneList
is a layout with a button I suggest actually using it as a layout route. Instead ofRouteOneList
rendering descendent routes it should instead render anOutlet
component for nested routes. This is so the mainRoutes
component can know of and match"/route1/:one/:two/:three"
and"/route1/add/:type/:id"
.Demo