I am trying to make a login flow using react. Routes are working as expected. But when I try to introduce nav bar, it is visible on all routes. I want to show nav bar only on selected components (only on profile and home route).
Any suggestions would be appreciated.
This is the Routes.js:
export default class Routes extends Component {
render() {
return (
<Switch>
<Route
exact
path='/*/home'
component={Home}
/>
<Route
exact
path='/*/login'
component={Login}
/>
<Route
exact
path='/*/profile'
component={Profile}
/>
<Route
exact
path='/'
component={Main}
/>
<Route
exact
path='/*/'
component={Org}
/>
</Switch>
);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
This is the App.js:
<div className="App">
<Nav />
<Routes />
</div>
To hide navbar in login page in React Router, we can put our nav bar inside the container with the authenticated routes. import React from "react"; const MyComponent = () => { //...
Go to your Navbar settings and find the navigation item you want to hide for a particular page. Click to edit and assign it a classname. You could assign it something like "hide-navigation-item."
To restrict access to routes in React Router, we set the render prop to a function that renders the component we want according to the condition we're checking. import { Route, Redirect } from "react-router"; <Route exact path="/" render={() => (loggedIn ?
Your Nav component is being rendered in every view because it's located outside your views. There are a couple of options here:
Nav into home & profile only. This would make sense if you wanted to keep your Routes component the same.withNav. This would be the best option if you need to render more than just home & profile with a Nav on top. Let's say you have a modal that needs to have the exact same Nav (for some reason), it may make sense to build an HOCNav, it may make sense to declare those two routes in a separate component.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With