3

Am new to ReactJS. I want to hide header component in Login page and show in inner pages. I have an App.js I have used ternary operator but not working.

class App extends Component {
    render(){
    let HideHeader = EmployeeLogin ? null : <HeaderNavContainer />
        return (
            <div>
                <Router history={history}>
                    <div>                    
                        {HideHeader}
                        <Switch>
                            <Route path="/about" component={About} />
                            <Route path="/EmployeeLogin" component={EmployeeLogin} />                        
                            <Route path="/MyPreferences" component={MyPreferences} />                        
                            <Route component={PageNotFound} />
                        </Switch>    
                    </div>    
                </Router>
            </div>
        );
    }
}

If EmployeeLogin component is rendered I want to hide header navigation <HeaderNavContainer /> if not I want to show <HeaderNavContainer />

4 Answers 4

15

In the render method of your HeaderNavContainer, you can do this:

render() {
  if (window.location.pathname === '/EmployeeLogin') return null;
  return <insert your header nav code>;
}

Since HeaderNavContainer is wrapped within <Router>, it'll re-render when window.location.pathname changes.

Alternatively, add HeaderNavContainer to your About, MyPreferences etc instead of putting in App.

Sign up to request clarification or add additional context in comments.

Comments

4

In component you can check if the history.location.pathname is equal to /EmployeeLogin and then return null. You can use withReducer for getting history object as a prop.

render(){
  if(this.props.history.location.pathname==='/EmployeeLogin'){
    return null;
  }   
  return (//your navigation component code.)
}

Comments

1

Instead of checking component exists or not try to check the URL is hit or not

In window.location.pathname you will get the current URL.

let HideHeader = window.location.pathname === 'your need string' ? null :

Comments

0

Create a HideHeader route that renders (conditionally) the Header component and an Outlet component for the nested route components.

import { Outlet, useLocation } from "react-router-dom";
import { Header } from "./Header";

const HideHeader = ({ hideHeaderPaths }) => {
const { pathName } = useLocation();

return (
 <>
   {!hideHeaderPaths.includes(pathName) && <Header />}
   <Outlet />
 </>
 );
};

export default HideHeader;

In App.js

import your HideHeader.js

import HideHeader from "./common/HideHeader"
...
...
<Route element={<HideHeader hideHeaderPaths={["/login"]} />}></Route>

and import your header on those pages where you want to show

<Header />

Comments

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.