DEV Community

Discussion on: How to detect images loaded in React

Collapse
aleksandrhovhannisyan profile image
Aleksandr Hovhannisyan

Note that this will run into a race condition if you set the src attribute on your images before you register their load listeners. I was able to reproduce this by refreshing the Codesandbox demo iframe; the images load very quickly because they were cached by the browser. Unfortunately, this means that by the time the event listener is registered, the load event will have already fired. So the message is never displayed.

See here for more context: stackoverflow.com/questions/146485....

Collapse
alejomartinez8 profile image
Alejandro Martinez Author • Edited on

Yes you're right, I had to use it in an app when the content comes from a CMS and we parse it, and normally the images aren't cached. I'm going to review it on this case. Thanks.