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

135
Visualizações
Infinite loop with a useEffect in React native

I have a problem, on the two functions below, when I check the debug of my request, there is an infinite loop of my GET request and I don't understand why my useEffect is re-triggering each time.

Here is my code :

const gardenData = async () => {
    setLoading(true);
    const user = await AsyncStorage.getItem('user');
    const parsedUserData = JSON.parse(user);
    try {
      const response = await axios.get(
        `http://127.0.0.1/api/garden?user=${parsedUserData.user.id}`,
        {
          headers: {
            Authorization: `Token ${parsedUserData.token}`,
          },
        },
      );
      if (response.status === 200) {
        setGarden(response.data);
        setLoading(false);
      }
    } catch (e) {
      console.log('Erreur ' + e);
      setLoading(false);
    }
  };

  const getPlotData = async () => {
    const user = await AsyncStorage.getItem('user');
    const parsedUserData = JSON.parse(user);
    const id = garden.map(g => g.id);
    alert(id);

    try {
      const response = await axios.get(
        `http://127.0.0.1/api/plots?garden=${id}`,
        {
          headers: {
            Authorization: `Token ${parsedUserData.token}`,
          },
        },
      );
      if (response.status === 200) {
        setPlot(response.data);
      }
    } catch (e) {
      alert(e);
    }
  };

  useEffect(() => {
    gardenData();
    getPlotData();
  }, [setGarden, setPlot]);

Thanks for the help provided

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

0

The array you pass in to the second parameter for useEffect should contain the state variables to watch that when changed will cause the useEffect to run again, because you are passing it [setGarden, setPlot], when you are using these methods to setPlot(response.data) then useEffect runs again, causing an infinite loop

If you want this to run just once pass useEffect an empty array like this:

useEffect(() => {
    gardenData();
    getPlotData();
  }, []);
about 4 years ago · Juan Pablo Isaza Relatório

0

If setGarden and setPlot are the state, I think you are watching on two vars who changes in the functions that you call. Just put empty array in the useEffect's watcher

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