Created
April 7, 2024 19:50
-
-
Save kenny-io/f8895f89dac8bff8308a636a6eb9ee77 to your computer and use it in GitHub Desktop.
This file contains 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
import React, { useState, useEffect } from 'react'; | |
const ImageLoader = ({ src, alt, placeholder }) => { | |
const [imageSrc, setImageSrc] = useState(placeholder || src); | |
useEffect(() => { | |
const img = new Image(); | |
img.src = src; | |
img.onload = () => setImageSrc(src); | |
}, [src]); | |
return <img src={imageSrc} alt={alt} />; | |
}; | |
export default ImageLoader; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment