I'm trying to load an image. In case the URL fails, onError I want to load a different image. The code works, but I'm using type "any" for the event. What is the correct type for the event?
function addDefaultSrc(ev: any) {
ev.target.src =
"default.png";
}
<img src={item.src} onError={addDefaultSrc} />
I would recommend you to use SyntheticEvent<HTMLImageElement, Event>
So you will be able to use it like this:
function addDefaultSrc(e: SyntheticEvent<HTMLImageElement, Event>) {
e.currentTarget.src = "default.png";
}
Also, you can read more about target
and currentTarget
here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With