javascript - React img onError event not triggered - Stack Overflow

In my create-react-app I have cards that display server-side images and I want to use a dummy image whe

In my create-react-app I have cards that display server-side images and I want to use a dummy image when no server-side image is returned. It seems that 'onError' event is never triggered. Here is my code:

import React from 'react';
import notfound from '../../icons/notfound.png';

class Card extends React.Component {
constructor(props) {
    super(props);
    this.state = {
        open: false
    }
}

addDefaultSrc(ev){
    ev.target.src = {notfound};
    ev.target.onerror = null;
}

render(){
    return (
        <div>
           <div><img className="item-photo" onError={() => this.addDefaultSrc} src={url} alt=""></img>
           </div>
        </div>
    );
}

and CardsList

    return (
         <Card
             key={i}
             id={item.No}
             url={`http://***.***.*.*:3000/${item.No}.JPG`} 
         />

Although I am getting 404 error (Not Found) when there is no image to be displayed, onError event is not triggered. Any help would be much appreciated.

In my create-react-app I have cards that display server-side images and I want to use a dummy image when no server-side image is returned. It seems that 'onError' event is never triggered. Here is my code:

import React from 'react';
import notfound from '../../icons/notfound.png';

class Card extends React.Component {
constructor(props) {
    super(props);
    this.state = {
        open: false
    }
}

addDefaultSrc(ev){
    ev.target.src = {notfound};
    ev.target.onerror = null;
}

render(){
    return (
        <div>
           <div><img className="item-photo" onError={() => this.addDefaultSrc} src={url} alt=""></img>
           </div>
        </div>
    );
}

and CardsList

    return (
         <Card
             key={i}
             id={item.No}
             url={`http://***.***.*.*:3000/${item.No}.JPG`} 
         />

Although I am getting 404 error (Not Found) when there is no image to be displayed, onError event is not triggered. Any help would be much appreciated.

Share Improve this question asked Mar 4, 2019 at 8:58 MorganaMorgana 3371 gold badge9 silver badges22 bronze badges 1
  • 1 It'd be better to define addDefaultSrc as an arrow function. Then you may pass it without binding: onError={this.addDefaultSrc} – hindmost Commented Mar 4, 2019 at 9:11
Add a ment  | 

1 Answer 1

Reset to default 3

You're not actually calling the function. Try one of these two options:

onError={this.addDefaultSrc} or onError={(e) => this.addDefaultSrc(e)}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745430002a4627349.html

相关推荐

  • javascript - React img onError event not triggered - Stack Overflow

    In my create-react-app I have cards that display server-side images and I want to use a dummy image whe

    5小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信