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

144
Views
Not Found page rendering for an entire page

Below in the App.js file , the routes component wraps part of the website , but i needed the NotFound component to be rendered for the whole page if a wrong url was entered , please advice on how this can be done in this case , i appreciate your feedback

const App = () => {
  return (
    <Box>
      <CssBaseline />
      <Navbar />
      <Routes>
        <Route path="/" element={<Navigate to="/home" />} />
        <Route path="/home" element={<Home />} />
        <Route path="/tours" element={<Tours />} />
        <Route path="/music" element={<Music />} />
        <Route path="/contact" element={<ContactUs />} />
      </Routes>
      <Testimonials />
      <Footer />
    </Box>
  );
};
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Use a layout route that renders the Navbar, Testimonials, Footer, and an Outlet for nested Route components to be rendered into. Render the NotFound component outside this layout route.

Read more about layout routes here.

Example:

import { Outlet } from 'react-router-dom';

const App Layout = () => (
  <>
    <Navbar />
    <Outlet /> // <-- nested routes render here
    <Testimonials />
    <Footer />
  </>
);

...

const App = () => {
  return (
    <Box>
      <CssBaseline />
      <Routes>
        <Route element={<AppLayout />}>
          <Route path="/home" element={<Home />} />
          <Route path="/tours" element={<Tours />} />
          <Route path="/music" element={<Music />} />
          <Route path="/contact" element={<ContactUs />} />
        </Route>
        <Route path="/" element={<Navigate to="/home" />} />
        <Route path="*" element={<NotFound />} />
      </Routes>
    </Box>
  );
};

If the NotFound component doesn't need the base CSS styling or Box container then move these into the AppLayout components as well.

about 4 years ago · Juan Pablo Isaza Report

0

You can simply use a wildcard path "*" after all valid routes. In this way it will 1st try to match the current user's path with the path provided inside <Route/> and if the path matches the existing route it will render that route's component. And when it will not match with your existing path then it will render the wildcard route and inside the wildcard route, you can render the 404 Not Found page. For example:

const App = () => {
  return (
    <Box>
      <CssBaseline />
      <Navbar />
      <Routes>
        <Route path="/" element={<Navigate to="/home" />} />
        <Route path="/home" element={<Home />} />
        <Route path="/tours" element={<Tours />} />
        <Route path="/music" element={<Music />} />
        <Route path="/contact" element={<ContactUs />} />
        <Route path="*" element={<NotFoundComponent/>} />
      </Routes>
      <Testimonials />
      <Footer />
    </Box>
  );
};

Note: The <Route path="*"/> should be placed after all existing routes or else your react app will render a 404 page every time

about 4 years ago · Juan Pablo Isaza Report

0

All you have to do is render a Route with a path = " * ", and React Router will make sure to only render the element if none of the other Routes match.

For react-router version 6

<BrowserRouter>
  <Routes>
    <Route path="/" exact element={<Home />}></Route>
    <Route path="*" element={<PageNotFound />}></Route>
  </Routes>
</BrowserRouter>

Show url as 404 of not found page

Redirect component has been removed from the react-router version 6. For react-router-dom v6, simply replace Redirect with Navigate

<Route path="/404" element={<PageNotFound></PageNotFound>} />
<Route path="*" element={<Navigate replace to="/404" />} />
 
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!