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

99
Vistas
setState in infinite loop inside render

How do I perform the same setState inside map but outside render / avoid the infinite loop that is:

Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested.

class MyComponent {
  render() {
    var images = [
      "https://i.picsum.photos/id/912/500/300.jpg?hmac=m-mTOMKlGcXQHDObNABzzeFPUPMuBVHr2l4rhrGjnmQ",
      "https://i.picsum.photos/id/912/500/300.jpg?hmac=m-mTOMKlGcXQHDObNABzzeFPUPMuBVHr2l4rhrGjnmQ",
      "https://i.picsum.photos/id/912/500/300.jpg?hmac=m-mTOMKlGcXQHDObNABzzeFPUPMuBVHr2l4rhrGjnmQ",
      "",
      "",
    ];
    return (
      <div class="row thumbnailview">
        {images.map((image, index) => {
          if (image.length < 2) {
            return (
              <div className="col thumbnails hidden">
                <img src={image} className="thumbnail" />
              </div>
            );
          }
          if (image.length >= 2) {
            return (
              <div className="col thumbnails">
                <img src={image} className="thumbnail" onClick={this.setState({ imageSrc: { image } })} />
              </div>
            );
          }
        })}
      </div>
    );
  }
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Your onClick should look like this:

onClick={() => this.setState({imageSrc:{image}})}

NOT this:

onClick={this.setState({imageSrc: {image}})}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You're calling setState during render in

onClick={this.setState({imageSrc: {image}})}

What you really want there is

onClick={() => this.setState({imageSrc: {image}})}

i.e. to wrap that setState in a function that only gets called when you click on the image.

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