Home > Software design >  React Router v6 not showing nested route - what am I missing?
React Router v6 not showing nested route - what am I missing?

Time:02-13

I have in my index the following routes:

    <BrowserRouter>
        <Routes>
            <Route path="/" element={<App />} />
            <Route path="admin" element={<AdminDashboard /> } />
        </Routes>
    </BrowserRouter>

I can access / and admin, and the components render correctly.

Inside AdminDashboard, I have two other routes:

        <Routes>
            <Route path="/" element={<AdminCards />} />
            <Route path="news-sources" element={<NewsSourcesAdmin /> } />
        </Routes>

If I access http://localhost/ and http://localhost/admin, I get things to render correctly.

However, if I attempt to go to http://localhost/admin/news-sources, then the NewsSourcesAdmin component does not render. I get an empty page instead.

Shouldn't the AdminDashboard component render given I'm under the admin route, and then the NewsSourcesAdmin component render inside of it given I am under the /admin/news-sources route?

I am on React 17, React Router v6.

What am I missing?

CodePudding user response:

v6 routes are exact by default. Use trailing * to match everything:

<Route path="admin/*" element={<AdminDashboard /> } />
  • Related