Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Nested routes in react router-dom not working

i don’t understand why nested routes is not working for my project.

Here’s what i’m doing

<Routes>
<Route index element={<Home />} />
<Route path="/chat" element={<Chat />} />
<Route path="/video-moments" element={<VideoMoments />} />
<Route path="/notifications" element={<Notifications />} />
// here's the route i'm trying to nest
<Route path="/profile" element={<Profile />} >
  <Route path="/edit-profile" element={<EditProfile />} />
</Route>

and then in my profile page i render an outlet like so:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

      <main className="profile-page">
    <LeftSideNav />
    <Outlet />
    {/* <ProfileContainer /> */}
    <RightSideNav />
  </main>

But it doesn’t work, instead the whole app breaks and it doesn’t display anything, what could be the problem?

>Solution :

You are declaring the nested route with an absolute path that isn’t a sub-route of the parent route.

Declare a full absolute path:

<Route path="/profile" element={<Profile />} >
  <Route path="/profile/edit-profile" element={<EditProfile />} />
</Route>

or to build a relative path, omit the leading slash "/":

<Route path="/profile" element={<Profile />} >
  <Route path="edit-profile" element={<EditProfile />} />
</Route>

The only differentiator between absolute and relative paths is the leading slash "/".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading