How to implement conditional routing i.e. if and only if some conditions satisfies, then routing should occur. For example, if and only if the user enters the correct credentials, login should be successful and the user should be able to see the welcome page.
If we directly hit some URL like localhost:8080/welcome
, that should not be navigated to welcome page. The welcome page should only be displayed after login.
How to achieve this, can anyone help me please?
App.js
import React, { Component } from 'react'; import Header from './Header'; class App extends Component{ render(){ return( <div> <Header /> </div> ); } } export default App;
Header.js
import React, { Component } from 'react'; import {Link} from 'react-router-dom'; import Login from './Login'; import SignUp from './SignUp'; class Header extends Component{ render(){ return( <div> <nav class="navbar navbar-default"> <div class="container-fluid"> <ul class="nav navbar-nav"> <li><Link to={Login}>Login</Link></li> <li><Link to={Login}>SignUp</Link></li> </ul> </div> </nav> </div> ); } } export default Header;
AllRoutes.js
import React, { Component } from 'react'; import { Switch, Route } from 'react-router-dom'; import Login from './Login'; import SignUp from './SignUp'; import Welcome from './Welcome'; class AllRoutes extends Component{ render(){ return( <Switch> <Route exact path="/login" component={Login} /> <Route exact path="/signup" component={SignUp} /> <Route exact path="/Welcome" component={Welcome} /> </Switch> ); } } export default AllRoutes;
Welcome.js
import React, { Component } from 'react'; class Welcome extends Component{ render(){ return( <div> <h2>Welcome to MainPage..</h2> </div> ); } } export default Welcome;
Firstly wrap all the content of your page inside the return function inside the . Next, create the induvial routes inside the component. For each route, we have the path and the element props, these tell the path on the address bar and the component to be rendered out respectively.
Definition. Conditional Route is defined as non-permanent ATS route or portion thereof which can be planned and used under specified conditions.
To help answer your question, I think you may need to also ask how that route should get blocked. Looking through the example above, you don't yet have a mechanism that helps answer the question of "should I be able to visit this page". That might come from state
, redux, or some other means of determining if the user is logged in.
Since react-router
is just plain React (one of my favorite parts!!) you have all the tools available to you that you would to conditionally show any part of your React app.
Here are a couple examples of how you might achieve this (by no means is this exhaustive. Be creative! It all depends on your requirements and the tools you are using)
class AllRoutes extends Component{ render(){ return( <Switch> <Route exact path="/login" component={Login} /> <Route exact path="/signup" component={SignUp} /> { this.state.authenticated && <Route exact path="/Welcome" component={Welcome} /> } </Switch> ); } }
One of my favorite ways to accomplish this is creating a ProtectedRoute
component
class ProtectedRoute extends Component { render() { const { component: Component, ...props } = this.props return ( <Route {...props} render={props => ( this.state.authenticated ? <Component {...props} /> : <Redirect to='/login' /> )} /> ) } } class AllRoutes extends Component { render() { return ( <Switch> <Route path='/login' component={Login} /> <ProtectedRoute path='/welcome' component={Welcome} /> </Switch> ) } }
While I didn't include any specific logic to how state.authenticated
was set, this may come from anywhere (by no means does it needs to come from state
). Do your best to answer the question of "how do I determine whether a user is authenticated" and use that mechanism as the means to handle route authentication.
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