title: How to correctly handle non PrivateRoutes in React?
body: <p>I have this piece of code in my app.js and have configured a PrivateRoute which requires the user to login and only allows access if the cookie is set. However, I would like to restrict users trying to hit <code>/login</code> after they have successfully logged in. I used the reverse logic of the PrivateRoute and created LoginRoute which serves the purpose but would like to know if there is a better approach.</p>
  
  <pre><code>import React from 'react';
  import {
    BrowserRouter as Router,
    Route,
    Switch,
    Redirect
  } from 'react-router-dom';
  import { ConnectedRouter } from 'connected-react-router'
  import cookies from 'cookies-js';
  
  import Home from './homeComponent';
  import Login from './loginComponent';
  import Dashboard from './dashboardComponent';
  import NoMatch from './noMatchComponent';
  
  const App = ({ history }) =&gt; {
    return (
      &lt;ConnectedRouter history={history}&gt;
          &lt;Switch&gt;
            &lt;Route exact={true} path="/" component={Home} /&gt;
            &lt;LoginRoute path="/login" component={Login} /&gt;
            &lt;PrivateRoute path="/dashboard" component={Dashboard} /&gt;
            &lt;Route component={NoMatch} /&gt;
          &lt;/Switch&gt;
      &lt;/ConnectedRouter&gt;
    );
  };
  
  const LoginRoute = ({ component: Component, rest }) =&gt; (
    &lt;Route {...rest} render={(props) =&gt; (
      cookies.get('access-token')
        ? &lt;Redirect to={{
            pathname: '/dashboard',
            state: { from: props.location }
          }} /&gt;
        : &lt;Component {...props} /&gt;
    )} /&gt;
  )
  
  const PrivateRoute = ({ component: Component, rest }) =&gt; (
    &lt;Route {...rest} render={(props) =&gt; (
      cookies.get('access-token')
        ? &lt;Component {...props} /&gt;
        : &lt;Redirect to={{
            pathname: '/login',
            state: { from: props.location }
          }} /&gt;
    )} /&gt;
  )
  
  export default App;
  </code></pre>