question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`onLoadingComplete()` should pass `naturalWidth` and `naturalHeight`

See original GitHub issue

What 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, …}

buggitybugbug

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
nickadamsoncommented, Jul 15, 2021
1reaction
styflecommented, Jul 29, 2021

If onLoadingComplete() passed back naturalWidth and naturalHeight, would that solve the problem?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found