0

I'm trying to hide the navbar from the sign-in page, but i can't find a way to do it. I'm new to react, can someone please help me? Thanks in advance :)

  1. This is my App.js component
    import React from "react";
    import { Route, Switch } from 'react-router-dom';

    
    class App extends React.Component {
          render() {
    
            return (
                <div>
                   
                    <NavBar />
                    <Switch>
                        <Route exact={true} path='/' component={HomePage} />
                        <Route path='/men' component={MensPage} />
                        <Route path='/woman' component={WomansPage} />
                        <Route path='/signin' component={SignInAndSignUpPage} />
                    </Switch>
                </div>
            )
    
        }
        };
    
        export default App;

  1. This is my index.js file
import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter } from 'react-router-dom';
import App from './app';

ReactDOM.render(
    <BrowserRouter>
        <App />
    </BrowserRouter>,
    document.getElementById('root')
);
mr_vmoore
  • 31
  • 1
  • 6

3 Answers3

1

Ciao, you could use withRouter provided by 'react-router-dom' in this way:

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


class App extends React.Component {
      render() {

        return (
            <div>
               
                {this.props.location.pathname !== '/signin' && <NavBar />}
                <Switch>
                    <Route exact={true} path='/' component={HomePage} />
                    <Route path='/men' component={MensPage} />
                    <Route path='/woman' component={WomansPage} />
                    <Route path='/signin' component={SignInAndSignUpPage} />
                </Switch>
            </div>
        )

    }
    };

    export default withRouter(App);

And hide NavBar in case this.props.location.pathname === '/signin'.

Giovanni Esposito
  • 10,696
  • 1
  • 14
  • 30
0

Try React Router's useLocation. You can then conditionally render based on location.

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

const App = () => {
  const { pathname } = useLocation();
  return (
    <div> 
     { pathname !== 'signin' && <NavBar /> }

Note, to use the Hook you will need to convert your class component to a function component. If you did not want to do this step, you could use React Router's withRouter HOC instead to access location:

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

class App extends React.Component {
  render() {
    const { location: { pathname } } = this.props;
    return (
      <div> 
       { pathname !== 'signin' && <NavBar /> }
         // ...
  }

export default withRouter(App);
Elise Chant
  • 5,048
  • 3
  • 29
  • 36
  • 1
    Thank you :). I will stick with withRouter, it seems appropriate for my needings now. Wish you a happy day – mr_vmoore Aug 19 '20 at 11:00
0

In react-router-dom v6 you can use outlet, I change your code to work with v6 like this:

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


class App extends React.Component {
  render() {

    return (
        <div>
            <Switch>
                <Route path="/signin"  element={<SignInAndSignUpPage />} />
                <Route
                element={
                    <>
                    <NavBar />
                    <Outlet />
                    </>
                }
                >
                <Route path="/"    exact element={<Dashboard />} />
                <Route path='/men'       element={<MensPage/>} />
                <Route path='/woman'     element={<WomansPage/>} />
            </Switch>
        </div>
    )

  }
};