0

Hello all I did use search but I couldnt find anything that solved this problem. I need two navbars one for most pages and one for a specific set of routes

code

  <BrowserRouter>
        <Header />
        <Switch>
          <Route exact path="/" component={() => <div>home</div>} />
          <Route exact path="/services" component={() => <div>services</div>} />
        </Switch>
  </BrowserRouter>

I need something that would say render all routes except /services/* because /services/* has different

I thought about changing things inside Header but there will be too many changes it would be easier to have different component.

  • You could create a higher-order component, something like `WithHeader`, that renders a component with the `Header` component. See [this related question](https://stackoverflow.com/questions/64306222/is-there-a-function-in-react-to-hide-a-component-based-on-the-website-path) that is about rendering a footer at some paths. – Yousaf Dec 06 '20 at 06:33
  • https://stackoverflow.com/questions/57789388/how-to-display-different-navbar-component-for-different-reactjs-pages this will solve your problem. – Abu Sufian Dec 06 '20 at 06:40

1 Answers1

0

You could set up a condition inside your path. I haven't tested this, but something in that nature could help you combine and separate certain routes.

<BrowserRouter>
        <Header />
        <Switch>
          <Route exact path={"/allComponents" || "/"} component={() => <div>home</div>} />
          <Route exact path="/services" component={() => <div>services</div>} />
        </Switch>
  </BrowserRouter>
Noah Kanyo
  • 500
  • 1
  • 3
  • 8