I am using ReactJS. My aim is to trigger some events when an image loads inside an SVG element.
Without SVG element, this code below works as expected -
function onImageLoad(event) {
console.log("Event triggered from <img> element", event);
// AS EXPECTED : Event object has natural width and natural height
}
return (
<div>
<img src={imageName} onLoad={ (event) => onImageLoad(event) }/>
</div>
)
However, this code does not work as expected -
function onImageLoad(event) {
console.log("Event triggered from <image> element", event);
// UNEXPECTED : Event object does not have natural width or natural height
}
return (
<div>
<svg>
<image href={imageName} onLoad={ (event) => onImageLoad(event) }/>
</svg>
</div>
)
I have tried reading in several places. Its not clear why <image> is different from <img> for onLoad() synthetic event in react.
You need to pass the event.currentTarget, not the event itself