Create LazyImage component
This commit is contained in:
parent
9d1de8173f
commit
92bdc1acf6
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;
|
||||
Loading…
x
Reference in New Issue
Block a user