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

138
Visualizações
404 page on Route not working as expected with children

I have a user route with two children.
I used path="*" in the case where the url does not match with a user so that it then returns UserNotFoundPage.

The problem is, while it does give me UserNotFoundPage when the url is .../user/someIncorrectUsername , it gives me an empty page if I only enter .../user/ or .../user . While I would have expected it to get me to the UserNotFoundPage.

How can I get this behavior ?

Here is my code:

<Router>
  <Routes>
    {/* PUBLIC */}
    <Route path="*" element={<Navigate replace to="/welcome"/>}/>
    <Route path="/welcome" element={<HomePage/>}/>
    <Route path="/dashboard" element={<DashboardPage/>}/>
    {/* RESTRICTED */}
    <Route path="/login" element={<LoginPage/>}/>
    {/* PRIVATE */}
    <Route path="/user" element={<Users/>}>
      <Route path="*" element={<UserNotFoundPage/>}/>
      <Route path="username" element={<UserProfilePage/>}>
    </Route>
    </Route>
  </Routes>
</Router>

function useAuth() {
  return true;
}

function HomePage() {
  return (
    <div>
      <h1>HOME PAGE</h1>
    </div>
  )
}

function UserNotFoundPage() {
  return (
    <div>
      <p>User not found</p>
    </div>
  )
}

function Users() {
  const auth = useAuth();
  return auth ? <Outlet/> : <Navigate to="/login"/>;
}

function UserProfilePage() {
  return (
    <div>
      <h4>USER PROFILE PAGE</h4>
    </div>
  )
}

function LoginPage() {
  return (
    <div>
      <h2>
        Login page
      </h2>
    </div>
  )
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The issue here is that abstractly "/user" also represents a route path that can be matched and rendered, via the Outlet, but there is no content to render on an exactly matched "/user" path.

An <Outlet> should be used in parent route elements to render their child route elements. This allows nested UI to show up when child routes are rendered. If the parent route matched exactly, it will render a child index route or nothing if there is no index route.

You can specify the UserNotFoundPage route to be the route that matches and renders on "/user" as well as on any non-matched "/user/*" path by specifying it to be the index route.

<Route path="/user" element={<Users/>}>
  <Route index path="*" element={<UserNotFoundPage/>}/>
  <Route path="username" element={<UserProfilePage/>}>
</Route>
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