2

I have problem with common header in react js. Currently login route is displaying common header and i dont want to show on my login page. If i go to contacts page than its showing common header which is perfect

import "./styles/App.scss";
import Navbar from "./components/elements/Navbar";
import Contacts from "./components/contacts/Contacts";
import { Provider } from "react-redux";
import store from "./store";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import AddContact from "./components/contacts/AddContact";
import EditContact from "./components/contacts/EditContact";
import Login from "./components/login/Login";
import Logout from "./components/logout/Logout";


function App(props) {
  return (
    <Provider store={ store }>
      <Router>
        <div className="App">
        {props.location.pathname !== '/login' ? <Navbar/> : null}
          <Route exact path="/login" component={ Login } />
          <div className="container">
            <div className="py-3">
              <Switch>
                <Route exact path="/" component={ Contacts } />
                <Route exact path="/logout" component={ Logout } />
                <Route exact path="/contacts/add" component={ AddContact } />
                <Route
                  exact
                  path="/contacts/edit/:id"
                  component={ EditContact }
                />
              </Switch>
            </div>
          </div>
        </div>
      </Router>
    </Provider>
  );
}

export default App;


  [1]: https://i.stack.imgur.com/7V0qi.png

2 Answers2

1

I think you meant to create Links in NavBar, not declare the Routes:

<Router>
  <div className="App">
    <div className="container">
      <div className="py-3">
        <Switch>
          <Route exact path="/login" component={Login} />
          <Route>
            <Navbar />
            <Switch>
              <PrivateRoute exact path="/" component={Contacts} />
              <PrivateRoute exact path="/logout" component={Logout} />
              <PrivateRoute exact path="/contacts/add" component={AddContact} />
              <PrivateRoute
                exact
                path="/contacts/edit/:id"
                component={EditContact}
              />
            </Switch>
          </Route>
        </Switch>
      </div>
    </div>
  </div>
</Router>

NavBar:

function Navbar() {
  const someId = 123 // example
  return (
   <>
    <Link to="/">Login</Link>
    <Link to="/logout">Logout</Link>
    <Link to="/contacts/add">Add Contacts</Link>
    <Link to={`/contacts/edit/${someId}`}>Edit Contact</Link>
   </>

  )
}

After this, you are most likely to be looking for authentication. I recently wrote an answer on authenticated or protected routes i.e. PrivateRoute.

Also, note that All children of a Switch should be Route or Redirect elements.

Ajeet Shah
  • 18,551
  • 8
  • 57
  • 87
1

You can only show the <Navbar/> when it's not the login page and you need to keep the login page inside the switch for the pages to work.

return (
    <Provider store={ store }>
      <Router>
        <div className="App">
          {props.location.pathname !== '/login' ? <Navbar/> : null}
          <div className="container">
            <div className="py-3">
              <Switch>
                <Route exact path="/login" component={ Login } />
                <Route exact path="/" component={ Contacts } />
                <Route exact path="/logout" component={ Logout } />
                <Route exact path="/contacts/add" component={ AddContact } />
                <Route
                  exact
                  path="/contacts/edit/:id"
                  component={ EditContact }
                />
              </Switch>
            </div>
          </div>
        </div>
      </Router>
    </Provider>
  );
Sean
  • 936
  • 4
  • 15