How to check for broken images in React JS

There is a native event for images called onerror that lets perform an action if the image cannot be loaded.

<img onError={this.addDefaultSrc} className="img-responsive" src={newsImage} alt={headline}/>

//in your component
addDefaultSrc(ev){
  ev.target.src="https://stackoverflow.com/questions/38527759/some default image url"
}

Leave a Comment