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

290
Views
React: how to hide a component on certain pages

How do i hide certain components on certain pages in my app? Specifically I need to hide Navbar and Header from the Settings page.

in App.js i set up a router:

<div>
  <Router>
    <Switch>
        <Header/>                               <- header and navbar are here
        <Navbar/>
        <Route exact path = "/" component= { Data } />
        <Route path = "/available-data" component= { Data } />
        <Route path = "/devices" component= { Devices } />
        <Route path = "/contacts" />
        <Route path = "/chat" />
        <Route path = "/settings" component = { Settings } />  <- i need to remove them from here 
    </Switch>
  </Router>
</div>
    

Header and Navbar are used in every component except in Settings. How do i go about removing/hiding them?

All three of the files are function components with useState hooks(if they even have state) if it matters :)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can render a second switcher component to only route to pages with the header and navbar UI.

// in App.js

<div>
  <Router>
    <Switch>
      <Route exact path="/settings" component={Settings} />
      <Route path='/' ><UiRouter /></Route>
    </Switch>
  </Router>
</div>


// in UiRouter.js

export default function UiRouter() {
  return (
    <>
      <Header />
      <Navbar />
      <Switch>
        <Route exact path="/" component={Data} />
        <Route path="/available-data" component={Data} />
        <Route path="/devices" component={Devices} />
        <Route path="/contacts" component={Contacts}/>
        <Route path="/chat" component={Chat}/>
      </Switch>
    </>
  );
}

Also, as Ajith mentioned, you should not have the components that you want to render for each route (Header and Navbar) inside the Switch component.

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!