Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!