Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

307
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda