react.js Replace img src onerror

后端 未结 22 3067
北恋
北恋 2020-11-29 00:20

I have a react component that is the detail view from a list.

I am trying to replace the image with a default image if the image does not exist and there is a 404 er

22条回答
  •  既然无缘
    2020-11-29 01:26

    For those like me who also wanted to change the styles of the element and/or change the img source, just do something like this:

    example {
         e.target.src = '/example/noimage.png' // some replacement image
         e.target.style = 'padding: 8px; margin: 16px' // inline styles in html format
      }}
    />
    

    Hope it helps!

提交回复
热议问题