Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

96
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!