TypeScript React <img /> onLoad event.target can't find naturalWidth naturalHeight

Jos*_*ang 4 html image typescript reactjs

在此输入图像描述

return <img {...props} onLoad={event => {
  console.log(event.target.naturalWidth)
}}/>
Run Code Online (Sandbox Code Playgroud)

I want to retrieve naturalWidth & naturalHeight in TypeScript React.

But got stuck, TypeScript can't find that property.

How do I retrieve naturalWidth & naturalHeight in TypeScript React?

miz*_*han 5

您可以HTMLImageElement通过event.currentTarget(而不是event.target)访问,所以这应该可以工作:

return <img {...props} onLoad={event => {
  console.log(event.currentTarget.naturalWidth)
}}/>
Run Code Online (Sandbox Code Playgroud)