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

280
Visualizações
Why I am getting re-render errors on loading page?

I have created an app that consists of a loading screen. However, if the API is failed after 5 seconds, a failure message is to be conveyed. I am getting too-many re-renders error on the line which I have mentioned in the code below.

I have used setTimeout function to replace the message of failure if API fails after 5 seconds of loading the page. The rest of my app functionality is working fine.

My app code is: -

function App() {

  //Is website loaded for first time?
  const [loadingFirstTime, setLoadingFirstTime] = useState(true);
  //Has the  site  loading  failed? If yes, pass that to loading component
  const [loadingFailed, setLoadingFailed] = useState(false);

  //To be run first time the website is loaded
  useEffect(() => {
    getMovies()
        .then(res => setMoviesDetails(res))
        .then(() => setLoadingFirstTime(false)); 
  }, [])

  ................... some code here .............


  //If the details of api haven't been loaded or api loading failed
  if (Object.keys(moviesDetails).length === 0 && loadingFirstTime) {

    //------**Error occurs here after 5 seconds as per the console**-----------------
    
    //check for the same thing after 5 seconds, if initial data still has been loaded?
    setTimeout(() => {
      if (Object.keys(moviesDetails).length === 0 && loadingFirstTime) {
        setLoadingFailed(true);
      }
    }, 5000);

    return (
      <LoadingScreen status={loadingFailed} />
    )
  }

  return (
    <>
      ........ App components which are working fine .............
    </>
  );
}

Code for my loading component: -

function LoadingScreen({status}) {

    const [loadingText, setLoadingText] = useState("Loading the site...");

    //check if site loading failed and display appropiate message
    if (status) {
        setLoadingText("Website loading failed. Please reload or contact the administrator.");
    }

    return (
        <div className="loading-screen">
            <h1>{loadingText}</h1>
        </div>
    )
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In React, you should avoid changing states when rendering, which is what you are doing in your LoadingScreen component when you are setting loadingText.

setLoadingText("Website loading failed. Please reload or contact the administrator.");

This is happening all the time you are passing a truthy value to status. That line is making LoadingScreen component to re-render itself again and again in an infinite loop.

Generally, it is better to implement this feature inside a useEffect function like this one:

function LoadingScreen({ status }) {
  const [loadingText, setLoadingText] = useState("Loading the site...");

  useEffect(() => {
    if (status) {
      const newLoadingText =
        "Website loading failed. Please reload or contact the administrator.";

      if (newLoadingText !== loadingText) {
        // This state setter provokes a re-render so set only if new value is
        // different than the actual value stored in state
        setLoadingText(newLoadingText);
      }
    }
  }, [loadingText, status]);

  return (
    <div className="loading-screen">
      <h1>{loadingText}</h1>
    </div>
  );
}
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