react.js Replace img src onerror

后端 未结 22 3059
北恋
北恋 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:16

    import OriginalImage from '../../originalImg.png'
    import ReplacementImage from '../../replaceImg.png'
    
    example {
        e.target.src = ReplacementImage //replacement image imported above
        e.target.style = 'padding: 8px; margin: 16px' // inline styles in html format
     }}
    />
    

    this is what I'm currently using.

提交回复
热议问题