`onLoadingComplete()` should pass `naturalWidth` and `naturalHeight`
See original GitHub issueWhat version of Next.js are you using?
11.0.2-canary.16
What version of Node.js are you using?
14.16.0
What browser are you using?
Brave (Chromium)
What operating system are you using?
Windows
How are you deploying your application?
next dev
Describe the Bug
onLoadingComplete() event handler does not pass any props.
Expected Behavior
The expected behavior for onLoadingComplete (unless I’m missing the point of this prop) is the same as onLoad for regular old HTML and onLoadedMetadata/onCanPlay for videos – a synthetic react event with .target prop.
To Reproduce
I have a function/event handler that calculates the aspect ratio of an image as soon as it is loaded:
const calcAspectRatio = (loadedMedia) => {
console.log("loadedMedia", loadedMedia)
let ratio;
let width;
let height; // ... you get the idea. bug can be replicated with a simple console.log though.
Here’s how I’ve implemented next/image:
<Image layout="fill" src={media} onLoadingComplete={calcAspectRatio} />
and videos:
<video muted autoPlay={autoPlay} loop onLoadedMetadata={calcAspectRatio}>
<source src={media} />
</video>
Currently the calcAspectRatio returns undefined
for next/image - afaiu, this would mean it doesn’t pass any reference to itself when loading is complete
And returns this (expected) for video:
SyntheticBaseEvent {_reactName: "onLoadedMetadata", _targetInst: null, type: "loadedmetadata", nativeEvent: Event, target: video.nfte__media-content, …}
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:10 (6 by maintainers)
Top Results From Across the Web
Next.js Image component props onLoadingComplete not ...
It appears that the next/image component is not invoking onLoadingComplete handler in case provided src is a data URI.
Read more >Nextjs image optimization with examples - Refine Dev
You should always add the width and height props in the image component when using remote images because NextJS cannot determine the images ......
Read more >Next.js automatic image optimization with next/image
Learn about automatic image optimization and how it can benefit developers with the native, game-changing, and powerful next/image API.
Read more >Image Optimization Through Next.js - Topcoder
Importing images dynamically using require() and await import() aren't ... To do so you have to pass priority prop to the image component....
Read more >HTMLImageElement.naturalWidth - Web APIs | MDN
The corresponding naturalHeight method returns the natural height of ... In such cases, naturalWidth will consider the width of the image ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
see https://github.com/vercel/next.js/discussions/27152 also
If
onLoadingComplete()
passed backnaturalWidth
andnaturalHeight
, would that solve the problem?