From e4302f4bb6afdec0d2593ac6895a088b1baa33c2 Mon Sep 17 00:00:00 2001 From: "Carlos A. Cabrera" Date: Wed, 26 Feb 2020 11:01:24 -0600 Subject: [PATCH] ammending useImage --- src/hooks/useImage.tsx | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/hooks/useImage.tsx b/src/hooks/useImage.tsx index 355f738..427444a 100644 --- a/src/hooks/useImage.tsx +++ b/src/hooks/useImage.tsx @@ -76,25 +76,25 @@ export const useImage = (ref: RefObject, image: Image, options?: LazyLoadOp }, [src, ref?.current, vHeight, vWidth, offsetX, offsetY, scrollX, scrollY]) useEffect(() => { - if (loaded && !ready) return - const img = new Image() - img.src = src - - /** - * Mark Image as loaded if loaded from cache - * http://mikefowler.me/journal/2014/04/22/cached-images-load-event - */ - if (img.complete) { - setSize({ width: img.width, height: img.height }) - setLoaded(true) - return - } - img.addEventListener('load', handleImageLoad) img.addEventListener('error', handleImageError) + if (!loaded && ready) { + img.src = src + + /** + * Mark Image as loaded if loaded from cache + * http://mikefowler.me/journal/2014/04/22/cached-images-load-event + */ + if (img.complete) { + setSize({ width: img.width, height: img.height }) + setLoaded(true) + return + } + } + return () => { img.removeEventListener('load', handleImageLoad) img.removeEventListener('error', handleImageError)