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

310
Visualizações
Why am I having too many re-renders in my React.useEffect()?

I'm still beginner using ReactJS and I'm not understanding a problem I'm having.

My useEffect is getting a list of data and after that I do a filter.

But when I save the data value of this filter, and with a console.log() I try to see the return value of my filter, there is an infinite loop of data saved being loaded and I don't understand why this is happening.

Can anyone help me with this?

Here's my code and it's more easy to understand what I mean:

And I put my code into codesandbox

print of console.log()

import React, { useRef } from "react";
import { Map, TileLayer } from "react-leaflet";

import { getAccommodations } from "./data";

import "./styles.css";

const App = () => {
  const center = [-27.592495455704718, -48.484572875610034];

  const mapRef = useRef();
  const [thing, setThing] = React.useState(0);
  const [accommodationData, setAccommodationData] = React.useState([]);

  React.useEffect(() => {
    if (mapRef.current) {
      const response = getAccommodations();

      const _response = response.filter((accommodation) => {
        return mapRef.current.leafletElement
          .getBounds()
          .contains([accommodation.listing.lat, accommodation.listing.lng]);
      });

      setAccommodationData(_response);
    }
  }, [center, thing, accommodationData]);

  // I commented this line to avoid too many re-renders
  // console.log("accommodationData: ", accommodationData);

  return (
    <Map
      ref={mapRef}
      style={{ width: "100%", height: "100vh" }}
      center={center}
      zoom={13}
      onmoveend={() => {
        setThing(thing + 1);
      }}
    >
      <TileLayer
        url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
      />
      {/* I'll add some <Markers> here with the accommodationData array */}
    </Map>
  );
};

export default App;

Thank you very much in advance

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

0

The useEffect hook keep trigger because you are changing the state accommodationData which is one of the dependencies of useEffect. So you have just to remove it from dependencies or add another state response and then change accommodationData when you have a response, example :

  const center = [-27.592495455704718, -48.484572875610034];

  const mapRef = useRef();
  const [thing, setThing] = React.useState(0);
  const [accommodationData, setAccommodationData] = React.useState([]);
  const [response, setResponse] = React.useState(null);

  React.useEffect(() => {
    if (mapRef.current && !response) {
      const response = getAccommodations();

      const _response = response.filter((accommodation) => {
        return mapRef.current.leafletElement
          .getBounds()
          .contains([accommodation.listing.lat, accommodation.listing.lng]);
      });

      setResponse(_response);
    }
  }, [center, thing, accommodationData]);
  
  React.useEffect(() => {
    if (response) setAccommodationData(_response);
  }, [response]);
  
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