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

180
Views
La función Javascript no se llama entre las navegaciones de página y el enrutamiento usando react-router-dom v6

Tengo una app.js algo como esto

 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> } />

y mi recurso de autenticación como este

 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;

He colocado una declaración de consola en la función getSessionId. Puedo ver los registros de la consola cuando la página se carga por primera vez. Pero cada vez que me muevo entre páginas usando navegar, no veo los registros de la consola, lo que infiero es que no se está llamando a isLoggedIn(). Por favor, ayúdame. Gracias de antemano

PS Así es como se definen el enrutamiento privado y el enrutamiento público

 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 answers
Answer question

0

Se debe llamar a la función isLoggedIn cuando se accede a la ruta y no cuando se procesa el componente Route . Mover invocando la función en los componentes de protección de ruta.

Ejemplo:

 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>

Editar javascript-función-no-ser-llamada-entre-página-navegaciones-y-enrutamiento-usando

about 4 years ago · Juan Pablo Isaza 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!