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

127
Views
React router - Show only one component (skip default components)

I have a <NotFound /> component which I would like to show if the user goes to the path /404. The only problem is I'd like to show only this component and not the other's such as <Nav /> and <Footer />.

Is there a way I can somehow return if the route is /404 so the other components aren't rendered?

function App() {
  return (
    <>
    <Router>
      // I'd like to show only the <NotFound /> component if path is /404
      // not <Nav /> or the div below.
      <Route path="/404">
            <NotFound />
      </Route>
      <Nav />
      <div className="App">
        <Switch>
          <Route path="/projects">
            <Projects />
          </Route>
          <Route path="/">
            <div className='container'>
              <Header />
              <hr />
              ...
            </div>
          </Route>
        </Switch>
      </div>
      <Footer />
    </Router>
    </>
  );
}

export default App;
almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Render the Nav and Footer on another Switch and Route components. If the path is "/404" then the NotFound component is rendered, otherwise, the "/" route is matched and rendered and the Switch and routes it renders will be matched conditionally as per usual.

Example:

function App() {
  return (
    <Router>
      <Switch>
        <Route path="/404" component={NotFound} />
        <Route path="/">
          <Nav />
          <div className="App">
            <Switch>
              <Route path="/projects" component={Projects} />
              <Route path="/">
                <div className='container'>
                  <Header />
                  <hr />
                  ...
                </div>
              </Route>
            </Switch>
          </div>
          <Footer />
        </Route>
      </Switch>
    </Router>
  );
}
almost 4 years ago · Santiago Trujillo 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!