mirror of
https://github.com/NohamR/gitprofile.git
synced 2026-05-25 20:00:25 +00:00
Create LazyImage component
This commit is contained in:
22
src/components/lazy-image/index.jsx
Normal file
22
src/components/lazy-image/index.jsx
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { useState, Fragment, useEffect } from 'react';
|
||||||
|
|
||||||
|
const LazyImage = ({ placeholder, src, alt, ...rest }) => {
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const imageToLoad = new Image();
|
||||||
|
imageToLoad.src = src;
|
||||||
|
|
||||||
|
imageToLoad.onload = () => {
|
||||||
|
setLoading(false);
|
||||||
|
};
|
||||||
|
}, [src]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Fragment>
|
||||||
|
{loading ? placeholder : <img src={src} alt={alt} {...rest} />}
|
||||||
|
</Fragment>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default LazyImage;
|
||||||
Reference in New Issue
Block a user