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

187
Visualizações
How to make my AuthContext wait for response before page loaded in (REACT)?

i made my context.js file to test if user is logged in :

export const myContext = createContext({});
export default function Context(props) {

    const [userObject, setUserObject] = useState();

    useEffect(() => {
        axios.get("http://localhost:3030/getuser", { withCredentials: true }).then((res) => {
           
            if (res.data) {
                setUserObject(res.data);
            }
        })
    }, [])
    return (
        <myContext.Provider value={userObject}>{props.children}</myContext.Provider>
    )
}

**everything is working perfect and im abla to login and logout ** , the only problem is when when i log in and refresh the page i still see the loginpage for a seconde (because its making the request to backend ) im using my context like this in :

const App = () => {
 const userObject = useContext(myContext);
 console.log("userObject: ",userObject);
  return (
    <div className="App">
      {/* <Header /> */}
      <UserBubble />
      <Home />
     { userObject ? (<h1>Welcome {userObject.email}</h1>) : <Login /> }
      
    </div>
  );
};
export default App;

so basically the userObject Load up as undefined or Null before it makes the request to backend and then will change back after the response to fix every thing ..

the Question is : how can i prevent that delay from happening and get the user object with the response without delaying that few seconds ... thank you **

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