Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

172
Vistas
Javascript function not being called between page navigations and routing using react-router-dom v6

I have an app.js something like this

class App extends React.Component {
    render() {
        return (
            <Routes>
                <Route path="/"
                    element={
                        <PrivateRoute
                            auth={{ isAuthenticated: AuthenticationResource.isLoggedIn() }}
                        >
                            {' '}
                            <Navigate to="/dashboard" />
                        </PrivateRoute>
                    }
                />
                <Route path="/login"
                    element={
                        <PublicRoute
                            auth={{ isAuthenticated: AuthenticationResource.isLoggedIn() }}
                        >
                            {' '}
                            <LoginPage />
                        </PublicRoute>
                    }
                />
                <Route path="/dashboard"
                    element={
                        <PrivateRoute
                            auth={{ isAuthenticated: AuthenticationResource.isLoggedIn() }}
                        >
                            {' '}
                            <DashboardPage />
                        </PrivateRoute>
                    }
                />

and my authentication resource like this

import StorageFactory from './storage';

const AuthenticationResource = (() => {
    const storage = StorageFactory();

    const getSessionId = function () {
        console.log('sessioncookie', storage.getItem());
        return storage.getItem();
    };

    const removeSessionId = function () {
        return storage.removeItem();
    };

    return {
        isLoggedIn: () => typeof getSessionId() === 'string' && getSessionId().length > 0,
    };
})();

export default AuthenticationResource;

I have placed a console statement in the getSessionId function. I am able to see the console logs when the page is loading the first time. But whenever I am moving between pages using navigate, I don't see the console logs, which I am inferring that the isLoggedIn() is not getting called. Please help me out. Thanks in Advance

P.S This is how the private routing and public routing are defined

const PublicRoute = ({ auth: { isAuthenticated }, children }) => {
    return isAuthenticated ? <Navigate to="/dashboard" /> : children;
};

const PrivateRoute = ({ auth: { isAuthenticated }, children }) => {
    return isAuthenticated === true ? children : <Navigate to="/login" />;
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The isLoggedIn function should be called when the route is accessed and not when the Route component rendered. Move invoking the function into the route protection components.

Example:

const PublicRoute = ({ auth: { isAuthenticated }, children }) => {
  return isAuthenticated() // <-- invoke here
    ? <Navigate to="/dashboard" />
    : children;
};

const PrivateRoute = ({ auth: { isAuthenticated }, children }) => {
  return isAuthenticated() // <-- invoke here
    ? children
    : <Navigate to="/login" />;
};

...

<Routes>
  <Route
    path="/"
    element={
      <PrivateRoute
        auth={{
          isAuthenticated: AuthenticationResource.isLoggedIn // <-- pass reference here
        }}
      >
        <Navigate to="/dashboard" />
      </PrivateRoute>
    }
  />
  <Route
    path="/login"
    element={
      <PublicRoute
        auth={{
          isAuthenticated: AuthenticationResource.isLoggedIn // <-- pass reference here
        }}
      >
        <LoginPage />
      </PublicRoute>
    }
  />
  <Route
    path="/dashboard"
    element={
      <PrivateRoute
        auth={{
          isAuthenticated: AuthenticationResource.isLoggedIn // <-- pass reference here
        }}
      >
        <DashboardPage />
      </PrivateRoute>
    }
  />
</Routes>

Edit javascript-function-not-being-called-between-page-navigations-and-routing-using

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda