Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for broken images in React JS

I'm writing a module that takes article data from json and shows a large image over the article text, a hero module as they say.

I've got the data and have set it up so if there is an image, it will show that image and if there is no image in the data, it will show a default image. Problem is that this method doesn't replace broken links to show the default image.

I'm still new to react and using state ... question is, should I be using state to check for the broken link and how do I do it?

This is how I get the data in as props in the module:

const { newsItemData: {
          headline = '',
          bylines = [],
          publishedDate: publishDate = '',
          updatedDate: updatedDate = '',
          link: newsLink = '',
          contentClassification: category = '',
          abstract: previewText = '',
          abstractimage: { filename: newsImage = '' } = {},
          surfaceable: { feature: { type: featureType = '' } = {} } = {},
        } = {},
        wideView,
        showPill,
        defaultImage } = this.props;

I display the info in this way:

<div className={imageContainerClassName} style={customBackgroundStyles}>
      {newsImage ? <img className="img-responsive" src={newsImage} alt={headline}/> : <img className="img-responsive" src={defaultImage} alt={headline}/>}
</div>

What should I do in order to also check for broken images? I think this is all the pertinent data needed, let me know if I should show anything else. Thanks!

like image 799
nyhunter77 Avatar asked Jul 22 '16 13:07

nyhunter77


People also ask

How do you know if a picture is broken?

There are plenty of ways to find broken images on your website. The easiest and quickest way would be to use dedicated services, as in Broken Link Checker, Free Link Checker, Xenu, Netpeak Spider, etc. You could also do an extensive SEO Site audit with Serpstat's Tool and its alternatives.

How do you check image is loaded or not in React?

forEach((image) => { image. addEventListener("load", () => updateStatus(imagesLoaded), { once: true }); image. addEventListener("error", () => updateStatus(imagesLoaded), { once: true }); }); return; }, [ref]); return status; };


2 Answers

Here is what I did to check if the image is broken. There is an attribute called onError which is called when the image is broken or cannot be loaded. For example, here is the img tag:

<img id={logo.id} src={logo.url} alt ={logo.name} onError={this.handleImageError} />

How I handled the error:

handleImageError = e => { //write your logic here.}
like image 34
dhellryder Avatar answered Oct 10 '22 06:10

dhellryder


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 = 'some default image url'
}
like image 195
eltonkamami Avatar answered Oct 10 '22 07:10

eltonkamami