Reputation: 4725
return <img {...props} onLoad={event => {
console.log(event.target.naturalWidth)
}}/>
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?
Upvotes: 5
Views: 4457
Reputation: 1415
I had to use a type assertion:
const { width, height } = e.currentTarget as HTMLImageElement;
Upvotes: 2
Reputation: 619
You can access HTMLImageElement
through event.currentTarget
(not event.target
) so this should work:
return <img {...props} onLoad={event => {
console.log(event.currentTarget.naturalWidth)
}}/>
Upvotes: 5
Reputation: 139
You can try innerWidth and innerHeight.
console.log(event.target.innerWidth)
Upvotes: 0