Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide navbar in login page in react router

I want to hide the navbar in a login page.

I did it actually, but I can't see the navbar on other pages.

This code is part of My App.jsx file.

I make history in App's state. And I hide navbar, when this pathname is '/' or '/login'.

It works!

But then I typed the ID and password, and clicked the login button, got 'success' result, and navigated to '/main'.

Now I can't see navbar in main component too.

How can I do this?

Sorry about my short english. If you can't understand my question, you can comment.

constructor(props) {   super(props);   this.state = {     isAlertOpen: false,     history: createBrowserHistory(),   };   this.toggleAlert = this.toggleAlert.bind(this); }  <BrowserRouter>   <div className="App">     {this.state.history.location.pathname === '/' || this.state.history.location.pathname === '/login' ? null       : <Header toggleAlert={this.toggleAlert} />}     <div className="container">       {this.state.history.location.pathname === '/' || this.state.history.location.pathname === '/login' ? null         : <Navbar />}       <Route exact path="/" render={() => <Redirect to="/login" />} />       <Route path="/login" component={Login} />       <Route path="/main" component={Main} />       <Route path="/user" component={User} />       <Route path="/hw-setting" component={Setting} />       <Route path="/hw-detail/:id" component={HwDetail} />       <Route path="/gas-detail/:id" component={GasDetail} />       {this.state.isAlertOpen ? <Alert /> : null}     </div>   </div> </BrowserRouter> 

login(event) {   event.preventDefault();   userService.login(this.state.id, this.state.password).subscribe(res => {     if (res.result === 'success') {       global.token = res.token;       this.props.history.push('/main');     } else {       alert(`[ERROR CODE : ${res.statusCode}] ${res.msg}`);     } }); 
like image 520
Jong-Hyen Kim Avatar asked Nov 14 '17 09:11

Jong-Hyen Kim


People also ask

How do I get navbar to not show on certain pages?

Go to your Navbar settings and find the navigation item you want to hide for a particular page. Click to edit and assign it a classname. You could assign it something like "hide-navigation-item."

How do I restrict navigation in react JS?

To restrict access to routes in React Router, we set the render prop to a function that renders the component we want according to the condition we're checking. import { Route, Redirect } from "react-router"; <Route exact path="/" render={() => (loggedIn ?

How do I turn off dom link on my react Router?

Set the pointer events CSS property to none to disable a Link in React, e.g. <Link style={{pointerEvents: 'none'}}> . When the pointer events property of the link is set to none , the link is disabled.

What is the BrowserRouter /> component?

BrowserRouter: BrowserRouter is a router implementation that uses the HTML5 history API(pushState, replaceState and the popstate event) to keep your UI in sync with the URL. It is the parent component that is used to store all of the other components.


2 Answers

You could structure your Routes differently so that the Login component doesn't have the Header Like

<BrowserRouter>   <Switch>   <div className="App">     <Route exact path="/(login)" component={LoginContainer}/>     <Route component={DefaultContainer}/>    </div>   </Switch> </BrowserRouter>  const LoginContainer = () => (   <div className="container">     <Route exact path="/" render={() => <Redirect to="/login" />} />     <Route path="/login" component={Login} />   </div> )    const DefaultContainer = () => (     <div>     <Header toggleAlert={this.toggleAlert} />     <div className="container">       <Navbar />       <Route path="/main" component={Main} />       <Route path="/user" component={User} />       <Route path="/hw-setting" component={Setting} />       <Route path="/hw-detail/:id" component={HwDetail} />       <Route path="/gas-detail/:id" component={GasDetail} />       {this.state.isAlertOpen ? <Alert /> : null}     </div>     </div>  ) 
like image 84
Shubham Khatri Avatar answered Sep 18 '22 07:09

Shubham Khatri


Simplest way is use div tag and put components in which you want navbar and put login route component outside div tag:

<div className="App">   <Router>      <Switch>       <Route exact path="/" component={Login} />       <div>         <NavBar />             <Route exact path="/addproduct" component={Addproduct}></Route>         <Route exact path="/products" component={Products}></Route>             </div>      </Switch>   </Router>  </div> 
like image 35
Vijit Nagar Avatar answered Sep 18 '22 07:09

Vijit Nagar