Skip to content

Commit 7d2934f

Browse files
committed
Fix progressive images not setting loaded state if pre-loaded
1 parent c7f04dd commit 7d2934f

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

src/components/ProgressiveImage.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use client';
22

33
import classNames from 'classnames';
4-
import React, { useEffect, useState } from 'react';
4+
import React, { useCallback, useEffect, useState } from 'react';
55
import LoadingSpinner from './LoadingSpinner';
66

77
export type ProgressiveImageProps = {
@@ -42,6 +42,12 @@ export default function ProgressiveImage({
4242
}: ProgressiveImageProps) {
4343
const [loaded, setLoaded] = useState(false);
4444

45+
const ref = useCallback((img: HTMLImageElement) => {
46+
if (img) {
47+
setLoaded(img.complete);
48+
}
49+
}, []);
50+
4551
useEffect(() => {
4652
if (loaded && onLoad) {
4753
onLoad();
@@ -74,6 +80,7 @@ export default function ProgressiveImage({
7480
onLoad={e => setLoaded(true)}
7581
width={width || imageProps.width}
7682
height={height || imageProps.height}
83+
ref={ref}
7784
src={src}
7885
/>
7986
{placeholder && (

0 commit comments

Comments
 (0)