Last active
May 17, 2025 22:26
-
-
Save jordymeow/ed083811e407e89b49c0eecd7f722695 to your computer and use it in GitHub Desktop.
Simple React Hook which handles the status (loading, loaded or failed) of your images. It does a bit of caching.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { useState, useEffect, useRef } = require('react'); | |
| function useImage({ src }) { | |
| let [ image, setImage ] = useState({ src: null, status: 'loading' }); | |
| let imageElement = useRef(); | |
| let loadedUrls = useRef(new Set()); | |
| useEffect(() => { | |
| const onload = () => { | |
| setImage(img => { | |
| loadedUrls.current.add(img.src); | |
| return { ...img, status: 'loaded' }; | |
| }); | |
| }; | |
| const onerror = () => { setImage(img => ({...img, status: 'failed'})) }; | |
| imageElement.current = document.createElement('img'); | |
| imageElement.current.addEventListener('load', onload); | |
| imageElement.current.addEventListener('error', onerror); | |
| return function cleanup() { | |
| imageElement.current.removeEventListener('load', onload); | |
| imageElement.current.removeEventListener('error', onerror); | |
| }; | |
| }, []); | |
| useEffect(() => { | |
| if (!src || loadedUrls.current.has(src)) { | |
| // No image, or already loaded. | |
| setImage({ src: src, status: 'loaded' }); | |
| } | |
| else { | |
| // New image to load. | |
| imageElement.current.src = src; | |
| setImage({ src: src, status: 'loading' }); | |
| } | |
| }, [src]); | |
| return { | |
| src: image.src, | |
| isLoading: image.status === 'loading', | |
| isLoaded: image.status === 'loaded', | |
| hasFailed: image.status === 'failed' | |
| }; | |
| }; | |
| export default useImage; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jordymeow btw, it has a memory leak: you need to clear the generated img element, right? I added returning the
clearfunction which is run in the caller component on unmount:const clear = useCallback(() => { if (imgEl.current) { imgEl.current.remove() } }, [])