Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an img tag, what is the type of an event fired by onError?

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} />
like image 329
Francisco Gutierrez Avatar asked Jan 01 '23 17:01

Francisco Gutierrez


1 Answers

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

like image 181
Viktor Zagoruyko Avatar answered Jan 08 '23 02:01

Viktor Zagoruyko