How to check for broken images in React JS

后端 未结 5 777
忘了有多久
忘了有多久 2020-12-05 19:25

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

5条回答
  •  北海茫月
    2020-12-05 19:58

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

    {headline}/
    
    //in your component
    addDefaultSrc(ev){
      ev.target.src = 'some default image url'
    }
    

提交回复
热议问题