Skip to content

🎥 Make videos programmatically with React

Merge branch 'img-delayrender-handle' into offthread-video

Jonny Burgerd6829810cd49
diff --cc packages/core/src/Img.tsx
index 592f19fa31,d3a06c5584..f5ee4386ae
--- a/packages/core/src/Img.tsx
+++ b/packages/core/src/Img.tsx
@@@ -15,14 -8,11 +15,16 @@@ const ImgRefForwarding: React.ForwardRe
HTMLImageElement
>
> = ({onLoad, onError, ...props}, ref) => {
- const [handle] = useState(() =>
- delayRender('Loading <Img> with src=' + props.src)
+ const [handle, setHandle] = useState<ReturnType<typeof delayRender> | null>(
+ () => {
+ return null;
+ }
);
+ const imageRef = useRef<HTMLImageElement>(null);
+
+ useImperativeHandle(ref, () => {
+ return imageRef.current as HTMLImageElement;
+ });
useEffect(() => {
if (
@@@ -57,16 -54,18 +66,20 @@@
[handle, onError]
);
- // If tag gets unmounted, clear pending handles because image is not going to load
+ // If image source switches, make new handle
useEffect(() => {
+ const newHandle = delayRender('Loading <Img> with src=' + props.src);
+ setHandle(newHandle);
+
+ // If tag gets unmounted, clear pending handles because image is not going to load
return () => {
- continueRender(handle);
+ continueRender(newHandle);
};
- }, [handle]);
+ }, [props.src]);
- return <img {...props} ref={ref} onLoad={didLoad} onError={didGetError} />;
+ return (
+ <img {...props} ref={imageRef} onLoad={didLoad} onError={didGetError} />
+ );
};
export const Img = forwardRef(ImgRefForwarding);