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.sstatic.net/7V0qi.png

2 Answers 2

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.

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

2 Comments

I have modify my code. Please check attach image
Its Solved! Thanks ajeet for your help!
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>
  );

6 Comments

Make sure you take in the props for the component. function App (props){}
Error is changed now -> TypeError: Cannot read property 'pathname' of undefined
Can you edit your question and show the whole component please. You should have props if you're using react router which it looks like you are
I have modified my question and also attached an image
Console.log your props and try and access the pathname and then use that to show the navbar
|

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.