Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

104
Visualizações
My React HOC logic is showing protected route briefly
import { useRouter } from "next/router";
import { useEffect } from "react";
import axios from "axios";

export default (ChildComponent) => {
  const composeComponent = (props) => {
    const router = useRouter();

    const getLoggedIn = async () => {
      const loggedInRes = await axios.get("http://localhost:5000/api/auth");
      if (loggedInRes.data === false) {
        router.push("/login");
      }
    };

    useEffect(() => {
      getLoggedIn();
    }, []);

    return (
      <div>
        <ChildComponent {...props} />
      </div>
    );
  };
  return composeComponent;
};

I made a GET request to my back-end which will return a false value if user clicks on protected route without being authenticated and then redirect them to the login page. But this logic keep showing the protected route briefly before redirecting the user to the login page. And this is a very bad user experience. Please I would like to know how I can Improve the logic and make sure the user did not see the protected route at all if they are not authenticated and they should also be redirected to the login route.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There are a number of ways you could go but the easiest is perhaps to add another 'isLoaded' state that is false to start with. Hide everything on your page whilst isLoaded is false, then check for auth and after you get a response set isLoaded to true.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda