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

133
Visualizações
Loading spinner while fetching an api in react app not showing

When the website loads for the first time the fetched data from an api don't show.But after one reload the products are shown . Tried to use a loading spinner while loading the data. The spinner won't show either. How can I show the the loaded data on the first time and while loading the products the Loading spinner will be shown.

const [isLoading, setIsLoading] = useState(false);


 useEffect(() => {
setIsLoading(true);

fetch(" https://hidden-citadel-3557.herokuapp.com/inventory")
  .then((res) => res.json())
  .then((data) => setInventories(data));
setIsLoading(false );  }, []); `
  if (isLoading) {
return <Loading></Loading>  }
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Move "setIsLoading(false)" to the second ".then", like this:

const [isLoading, setIsLoading] = useState(false);


 useEffect(() => {
   setIsLoading(true);

fetch(" https://hidden-citadel-3557.herokuapp.com/inventory")
  .then((res) => res.json())
  .then((data) => {
     setIsLoading(false);
     setInventories(data)
   });
 }, []); 

  if (isLoading) {
return <Loading></Loading>  }
about 4 years ago · Juan Pablo Isaza Relatório

0

Based on your code, your approach is not a very viable one.

Here's what I would do.

  1. Use a separate function for loading the data.
function loadData()
{
   setLoading(true);
   fetch(" https://hidden-citadel-3557.herokuapp.com/inventory")
     .then((res) => res.json())
     .then((data) => {
        setInventories(data);
        setLoading(false);
     });
}
  1. Then perform your effect:
 useEffect(loadData,[]);
  1. In your jsx, use the loading variable to render <loading> element based on loading state varibale value:

{ loading ? <loading></loading> : ""}

about 4 years ago · Juan Pablo Isaza Relatório

0

This works for me:

import React, { useState, useEffect } from "react";

const Index = () => {
  const [isLoading, setIsLoading] = useState(true);

  useEffect(() => {
    const fetchData = async () => {
      const response = await fetch(
        "https://627a5a9773bad50685876437.mockapi.io/users"
      );
      await response.json();
      setIsLoading(false);
    };
    fetchData();
  }, []);

  return isLoading ? <p>Loading</p> : <p>Loaded!</p>;
};

export default Index;
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