So there are actually a couple of approaches here. I assume you are using react-router-dom for extracting Route component and you directly render App component such as ReactDOM.render(<App />, rootElement);
If that is the case, the fastest solution based on your code snippet is that
const App = () => {
const pathname = window.location.pathname
return (
<Container fluid>
<Row>
{pathname === '/login' ?
<Col lg="2">
<Sidebar />
</Col> : null}
<Col lg="10">
<main>
<Route exact path="/" component={Home} />
<Route exact path="/login" component={Login} />
</main>
</Col>
</Row>
</Container>)
}
If, you App component is used such as <Route exact path="somePath" component={App} />
Or, exported after being wrapped by withRouter Higher Order Component provided by react-router-dom, passing history, location and match props automatically handled by the routing library so you do the following update
const App = ({location: {pathname}}) => {
return (
<Container fluid>
<Row>
{pathname === '/login' ?
<Col lg="2">
<Sidebar />
</Col> : null}
<Col lg="10">
<main>
<Route exact path="/" component={Home} />
<Route exact path="/login" component={Login} />
</main>
</Col>
</Row>
</Container>)
}
I am assuming that, currently your code works the way you expect and your only concern is rendering SiderBar conditionally.
However, when setting up multiple routes, I would suggest utilizing Switch component provided by react-router-dom https://reacttraining.com/react-router/web/api/Switch
Also, instead of dynamically show/display SideBar component based on pathname, I would probably create a component such as MainLayout in the following way
const MainLayout = ({children}) => <div><SideBar/>{children}</div>
And update my Home component such as
const Home = () => <MainLayout>{content related to home page}</MainLayout>
So by this way, the SideBar would only be visible in the pages that you want it to be visible without checking pathname