Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

363
Visualizações
React Router 6 Nest Routes

I have a protected route that wraps my layout route for my other components that uses the layout component.

Im having an issue with the protected route not working as expected. If a user is null, when i try to access localhost:3000/create for example it should render my landing page but instead i get a blank screen.

I realised if i only have one route that contains one element prop it works fine. What am i doing wrong?

My Routes

<Router>
  <Routes>
    <Route path='/' element={<LandingPage />} />
    <Route path='*' element={<NotFound />} />
    <Route element={<ProtectedRoutes />}>
      <Route element={<ResponsiveDrawer />}>
        <Route path='/dashboard' element={<Dashboard />} />
        <Route path='/create' element={<Create />} />
        <Route path='/edit/:id' element={<Edit />} />
      </Route>
    </Route>
  </Routes>
</Router>

My Protected Routes

const ProtectedRoutes = () => {
 const { user } = useContext(UserContext);
 // console.log(user);

 return user !== null ? <Outlet /> : <LandingPage />;
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The ProtectedRoutes is rendering an Outlet component for nested Route components to be rendered into. The ResponsiveDrawer also needs to render an Outlet for any nested Route components it is wrapping.

<Router>
  <Routes>
    <Route path='/' element={<LandingPage />} />
    <Route path='*' element={<NotFound />} />
    <Route element={<ProtectedRoutes />}>    // <-- render Outlet
      <Route element={<ResponsiveDrawer />}> // <-- render Outlet
        <Route path='/dashboard' element={<Dashboard />} />
        <Route path='/create' element={<Create />} />
        <Route path='/edit/:id' element={<Edit />} />
      </Route>
    </Route>
  </Routes>
</Router>

The route rendering the ResponsiveDrawer is rendered into the ProtectedRoutes component's Outlet, and nested routes are rendered into the ResponsiveDrawer component's Outlet.

Example ResponsiveDrawer:

const ResponsiveDrawer = () => {
  // business logic

  return (
    <>
      {/* UI */}
      {/* Drawer UI */}
      <Outlet /> // <-- nested routes render here
    </>
  );
};
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda