Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
Question about PrivateRoutes and to get navigated back to login page

Hi I need help getting my code to work so that when I try to log back in I won't be able to view the dashboard since I logged out. Right now its giving me a blank screen in my project and I think its because privateroute isn't a thing anymore? Not sure. This is my code in PrivateRoute.js:

import React, { Component } from 'react';
import { Route, Navigate } from 'react-router-dom';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';

const PrivateRoute = ({ component: Component, auth: { isAuthenticated, loading },
...rest }) => (
    <Route {...rest} render={props => !isAuthenticated && !loading ? (<Navigate to='/login' />) : (<Component {...props} />)} />
)


PrivateRoute.propTypes = {
  auth: PropTypes.object.isRequired
};

const mapStatetoProps = state => ({
    auth: state.auth
});

export default connect(mapStatetoProps)(PrivateRoute);

This is the code for the app.js:

import React, { Fragment, useEffect } from 'react';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import Navbar from './components/layout/Navbar';
import Landing from './components/layout/Landing';
import Register from './components/auth/Register';
import Login from './components/auth/Login';
import Alert from './components/layout/Alert';
import Dashboard from './components/dashboard/Dashboard';
import PrivateRoute from './components/routing/PrivateRoute';
// Redux
import { Provider } from 'react-redux';
import store from './store';
import { loadUser } from './actions/auth';
import setAuthToken from './utils/setAuthToken';

import './App.css';

if (localStorage.token) {
  setAuthToken(localStorage.token);
}

const App = () => { 
  useEffect(() => {
    store.dispatch(loadUser());
  }, []);

return (
  <Provider store={store}>
  <Router>
    <Fragment>
      <Navbar />
      <Routes>
      <Route exact path='/' element={<Landing/>} />
      </Routes>
      <section className="container">
        <Alert />
        <Routes>
          <Route exact path='/Register' element={<Register/>} />
          <Route exact path='/Login' element={<Login/>} />
          <PrivateRoute exact path='/dashboard' element={Dashboard} />
        </Routes>
      </section>
    </Fragment>
  </Router>
  </Provider>
)};

export default App;

Please Help!

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

there is a recommended Protected Route implementation in the v6 react router docs you can follow.

DOCS IMPLEMENTATION

Your issue:

<Route {...rest} render={props => !isAuthenticated && !loading ?
 (<Navigate to='/login' />) : (<Component {...props} />)} />

This Route is not up to date with v6, which I assume you are using, you need to update it.

Exact is also not supported anymore, there is no point in including it in your routes.

If you are not using v6 please update your post and include your react-router version.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!