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

221
Vistas
Redux store takes lil bit time to update store which causes else block to execute and hence get redirected to unintended page
//PrivateAuthComponent.js
import React, { useEffect, useState } from "react";
import { useSelector } from "react-redux";
import { useRouter } from "next/router";

const PrivateAuth = ({ children }) => {
  const router = useRouter();
  const [userLoaded, setUserLoaded] = useState(false);
  const userExists = useSelector((state) => state.user);

  console.log("User in private auth", userExists);

  useEffect(() => {
   
    if (userExists && userExists.token) {
      setUserLoaded(true);
    } else {
      router.replace("/login");
      setUserLoaded(false);
    }
  }, []);

  return userLoaded && <>{children}</>;
};

export default PrivateAuth;

//UserHistory.js
import React from "react";
import PrivateAuth from "../../auth/privateAuth";

const history = () => {
  return (
    <PrivateAuth>
      <div className="container-fluid">
        <div className="row">User Page</div>
      </div>
    </PrivateAuth>
  );
};

export default history;

I am trying to wrap some pages so that they can't be accessed by users who are not logged in. When I login, I get redirected to user/history page as intended. But I manually want to go to user/history for the second time, the page reloads and what happens is redux store takes a bit time to update itself, but that code inside useEffect runs before this happens which causes that block inside else to run. So I get redirected to "/login" page because users don't exist yet and hence never get back to the user/history page as intended even when I am logged in and redux store is updated. What can I do about this?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I would suggest adding a loading state when the API call is happening,

Loading is true show loader If the user does not exist redirect to /login else render children

const PrivateAuth = ({ children }) => {
  const { user, loading } = useSelector((state) => state);
  if (loading) return <Loader />;
  if (!user) return <Redirect to="/login" />;
  return <>{children}</>;
};
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