2

I want to making conditional Header page by page, which means that I don't want to show Header at Home page and I want to show Header at the other pages like posts page, projects page, etc..

I've tried to think how can I solved this problem, but there was no special way to working with react-router-dom. Of course, it may be only hard for me.

Whatever, I can't solve above problem is same.

So, this is current code:

<div className="ui container">
  <BrowserRouter>
    <Header />
    <Route exact path="/">
    <Route exact path="/posts">
    <Route exact path="/project">
    <Route exact path="/profile">
  </BrowserRouter>
</div>

and I want fix it like this:

<div className="ui container">
  <BrowserRouter>
    {() => { 
      if(path !=="/") 
        return <Header />;
      } else {
        return ;
    }}
    <Route exact path="/">
    <Route exact path="/posts">
    <Route exact path="/project">
    <Route exact path="/profile">
  </BrowserRouter>
</div>

3 Answers 3

4

without Switch , you can do this :

 <BrowserRouter>
    {window.location.pathname !== "/" ? <Header /> : null}
    <Route exact path="/">
    <Route exact path="/posts">
    <Route exact path="/project">
    <Route exact path="/profile">
  </BrowserRouter>
Sign up to request clarification or add additional context in comments.

2 Comments

This is It! thanks! for answer. It worked with your code!
humm.... I think this code makes another problem. Header doesn't render although link was changed. It shows Header within "/(something)", if I access with "/' then it doesn't render header. here is deme link acd2ef3d.ngrok.io
1

try with a Switch:

import { Route, Switch } from 'react-router-dom';

...

<Switch>
      <Route exact path="/" component={null} />
      <Route component={Header} />
<Switch>

...

doc: https://reacttraining.com/react-router/web/api/Switch

Comments

1

There are ways you can do it.

  • You can use withRouter(), an Higher Order Component to wrap your component. So, with that that component will have access to the BrowserRouter props.

  • You can use Redux, We the posts page/component is mounted you set a isheader state to false. then you can use this state to condition the Header component.

A reference link:- Hide header on some pages in react-router-dom

Another method is to wrap those route that will have the header with a parent component example:

const Layout = ({ children }) => (
            <div>
              <Header />
                {children}
            </div>
          )
        const App = ()=> (
            <BrowserRouter>
                <Switch>
                    <Route exact path="/" component={Home} />
                    <Layout>
                      <Route exact path="/posts" component={..}>
                      <Route exact path="/project" component={..}>
                      <Route exact path="/profile" component={..}>
                    </Layout>
                </Switch>
            </BrowserRouter>
    )

1 Comment

Thank for kind anwser!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.