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

166
Visualizações
Infinite load time on mobile device

I am trying to display a loader till the page finishes rendering and for that I used this code:

export default function App() {
  const [isLoading, setIsLoading] = React.useState(true);

  const handleLoading = () => {
    setIsLoading(false);
  };

  useEffect(() => {
    window.addEventListener('load', handleLoading);
    return () => window.removeEventListener('load', handleLoading);
  }, []);

  return (
    <div>
      {isLoading ? (
        <Load />
      ) : (
        <div className="app">
          <Topbar />
          <div className="parts">
            <Intro />
            <About />
            <Projects />
            <Contact />
          </div>
        </div>
      )}
    </div>
  );
}

This works fine on desktop devices except in incognito mode and in mobile devices. It shows the loader forever and when I press the refresh button twice only then will the page appear. I tested the site on gtmetrix and I got this error: Lighthouse Error: The page did not paint any content. How do I solve this?

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

0

useEffect will get called when your state has changed, right?

then when do you think your useEffect will be called?

the component is created, the loading state is set to true, and everything is working good, why would your component calls your useEffect when the state is not even changing?

i have two suggestions:

  1. remove your 'load' event listener and add it to your main function code, this way the event listener will be initiated. then when the page loads, it will set your state into false, thus your useEffect will be called, and it removes the event listener.

  2. is to use class based components with lifecycle hooks that are designed for what you are trying to do.

about 4 years ago · Juan Pablo Isaza Relatório

0

I'm not sure that eventListener works fine in your case but if I were you I would simply setIsLoading(false) in useEffect and if you want it to stop before Component mounts you can do it in this way :

export default function App() {
const [isLoading, setIsLoading] = React.useState(true);
const [mounted,setMounted] = React.useState(false);

if(!mounted) setIsLoading(false)

useEffect(() => {
    setMounted(true)
}, []);

return (
    <div>
        {isLoading ? (
            <Load />
        ) : (
            <div className="app">
                <Topbar />
                <div className="parts">
                    <Intro />
                    <About />
                    <Projects />
                    <Contact />
                </div>
            </div>
        )}
    </div>
);

}

hope it was helpful ;)

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