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

What is wrong with this nested routing?

I’m trying to implement nested routing in a react app, using this code:

  <>
      <Navbar />
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="plans" element={<Plans />} />
        <Route path="trainers" element={<Trainers />} />
        <Route path="store" element={<StoreMain />} />
        <Route path="myportal" element={<MyPortal />}>
          <Route path="workouts" element={<WorkoutsFetch />} />
        </Route>
        <Route path="login" element={<Login />} />
        <Route path="signup" element={<Signup />} />
      </Routes>
    </>

In the MyPortal component there is a link where it leads the user to workouts component. This component renders a div, which contains fetched data. The components itself works flawlessly, but the part that doesn’t work is when I try to go to that route – it doesn’t render anything. The URL changes to /myportal/workouts, but the pages doesn’t updates. Any suggestions?

I looked into other Stack Overflow solutions where I tried to do this:

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

<Route path="myportal/*" element={<MyPortal />}> // - with /* after the path
  <Route path="workouts" element={<WorkoutsFetch />} />
</Route>

But this didn’t help my problem.

>Solution :

Since your route is a child of myportal, you’ll need to index it, putting it as:
<Route index element={} /> should resolve your issue.

Make sure you also have an outlet in the parent: https://reactrouter.com/en/main/components/outlet

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