It seems I can not get around this problem. What I'm trying to do is attempt React to verify that the image does not return 404 to display an alternate image. Something like the one below does not work, since React does not wait for the image to try to load to return the component.
getInitialState: function () { var img = new Image(); img.onerror = function() { img.src = "alternativeImage.jpg" }, img.src = this.props.image; return {image: <img src={img.src} />}; },
In the above code, the images will only display perfectly, but the 404 alternate image is not displayed.
I tried putting this in another method outside of getInitialState. I also tried to get it to call an external method outside of the React component, but nothing works.
There is a short method for adding the onerror attribute to the image tag itself, but it seems that React has the same problem as not executing this code and / or updating the result itself based on the result.
I think the most annoying part is that I don't have a function called so that React can work with a JavaScript image object.
Thanks for any help you can provide.
source share