Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Route Component handle all "/" routes EXCEPT for "/login"

Dashboard component renders too, while I am entering /login. I need to omit Dashboard component while in Login.

Exact attribute does not fit because Dashboard has nested paths like /dashboard/users, /dashboard/settings

<BrowserRouter>
    <Route path='/'>
         <div>
             <Route path='/login'   component={Login}       exact   />
             <Route path='/'        component={Dashboard}           />
         </div>
    </Route>
</BrowserRouter>   
like image 476
Firanolfind Avatar asked Aug 09 '17 12:08

Firanolfind


People also ask

How do you restrict a path in react?

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 ?

What is a protected route?

Private Routes in React Router (also called Protected Routes) require a user being authorized to visit a route (read: page). So if a user is not authorized for a specific page, they cannot access it.

How do you prevent a browser from going back to login form page once user is logged in react JS?

I succeeded in preventing my website from going back to the login page by pressing the back button using history.


1 Answers

You could use Switch to render the first matching Route

<BrowserRouter>
    <Route path='/'>
         <div>
            <Switch>
              <Route path='/login'   component={Login}       exact   />
              <Route path='/'        component={Dashboard}           />
            </Switch>
         </div>
    </Route>
</BrowserRouter>
like image 120
Yury Tarabanko Avatar answered Oct 18 '22 08:10

Yury Tarabanko