Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally rendering component based on routing in react

Tags:

reactjs

Hello how i can conditionally rendering my component based on routing?

example of my app.js

const App = () => (
    <Container fluid>
        <Row>
          <Col lg="2">
            <Sidebar />
          </Col>
          <Col lg="10">
            <main>
              <Route exact path="/" component={Home} />
              <Route exact path="/login" component={Login} />
            </main>
          </Col>
        </Row>
    </Container>      
)

in this case i want to hide my sidebar component if routing is /login

like image 267
user3348410 Avatar asked Oct 20 '25 21:10

user3348410


1 Answers

You could add a Switch which renders nothing for the /login route, but renders the Sidebar for every other route.

const App = () => (
  <Container fluid>
    <Row>
      <Col lg="2">
        <Switch>
          <Route path="/login" />
          <Route path="/" component={Sidebar} />
        </Switch>
      </Col>
      <Col lg="10">
        <main>
          <Switch>
            <Route exact path="/" component={Home} />
            <Route path="/login" component={Login} />
          </Switch>
        </main>
      </Col>
    </Row>
  </Container>
);
like image 157
Tholle Avatar answered Oct 22 '25 12:10

Tholle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!