Skip to content

Instantly share code, notes, and snippets.

@kenny-io
Created April 7, 2024 19:50
Show Gist options
  • Save kenny-io/f8895f89dac8bff8308a636a6eb9ee77 to your computer and use it in GitHub Desktop.
Save kenny-io/f8895f89dac8bff8308a636a6eb9ee77 to your computer and use it in GitHub Desktop.
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