reactjsreact-routing

How to redirect in react-router when routes are in different files


I was trying to redirect to the login page if the user is not logged in. It was working fine when I was using routes in App.js but after making seperate file for routing I am not.

route.js

import React from 'react';
import Login from './Login';
import Register from './Register';
import Home from './Home';
import './style.css';
import { Route, Switch } from 'react-router-dom';

const routes = (
        <Switch>
            <Route key="0" path="/" exact render={ props => <Home {...props} />} />
            <Route key="1" path="/login" render={ props => <Login {...props} />} />
            <Route key="2" path="/register" render={ props => <Register {...props} />}  />
            <Route key="2" path="/home"  render={ props => <Home {...props} /> } />
        </Switch>
        );

export default routes;

App.js

class App extends Component {
  state={
    isLogin:false
  }
  render() {
    return (

      <div className="App">
      <Router>
      <Header/>
    <div className="container">
      {routes}

    </div>
    </Router>

      </div>
    );
  }
}

My previous rout conditions was

const routes = (
           <Switch>
            <Route key="0" path="/" exact render={ () => this.state.login ? ( <Home/> ): ( <Redirect to="/login"/> )} />} />
            <Route key="1" path="/login" render={ props => <Login {...props} />} />
            <Route key="2" path="/register" render={ props => <Register {...props} />}  />
            <Route key="2" path="/home"  render={ () => this.state.login ? ( <Home/> ): ( <Redirect to="/login"/> )} />
          </Switch>
        );

SO I want these routes to be working.


Solution

  • I made a small sandbox to show you how to do it, we create routes as a function that takes in the isLogin state from App and based on that value we render the right component.

    const Home = () => <h1>Home</h1>;
    const Login = () => <h1>Login</h1>;
    const Register = () => <h1>Register</h1>;
    
    const Nav = () => (
      <div>
        <Link to="/">Home</Link>
        <Link to="/login">Login</Link>
        <Link to="/register">Register</Link>
      </div>
    );
    // Takes isLogged from this.state
    const routes = isLogged => (
      <Switch>
        <Route
          path="/"
          exact
          render={() => (isLogged ? <Home /> : <Redirect to="/login" />)}
        />
        } />
        <Route path="/login" render={props => <Login {...props} />} />
        <Route path="/register" render={props => <Register {...props} />} />
        <Route
          path="/home"
          render={() => (isLogged ? <Home /> : <Redirect to="/login" />)}
        />
      </Switch>
    );
    
    class App extends Component {
      state = {
        isLogin: false
      };
    
      toggleLogin = () =>
        this.setState(prevState => ({
          isLogin: !prevState.isLogin
        }));
    
      render() {
        const { isLogin } = this.state;
    
        return (
          <Router>
            <div className="App">
              <button onClick={this.toggleLogin}>Toggle Login</button>
              <Nav />
              <h1>{`isLogin ${isLogin}`}</h1>
              {/* invoke routes with isLogin */}
              {routes(isLogin)}
            </div>
          </Router>
        );
      }
    }
    

    SandBox