TypeScript React <img /> onLoad event.target找不到naturalWidth naturalHeight

时间:2019-06-04 06:44:41

标签: html reactjs image typescript

enter image description here

return <img {...props} onLoad={event => {
  console.log(event.target.naturalWidth)
}}/>

我想在TypeScript React中检索naturalWidth和naturalHeight。

但是卡住了,TypeScript找不到该属性。

如何在TypeScript React中检索naturalWidth和naturalHeight?

2 个答案:

答案 0 :(得分:2)

您可以通过HTMLImageElement(而非event.currentTarget)访问event.target,因此应该可以:

return <img {...props} onLoad={event => {
  console.log(event.currentTarget.naturalWidth)
}}/>

答案 1 :(得分:0)

您可以尝试innerWidth和innerHeight。

console.log(event.target.innerWidth)