Skip to content

Instantly share code, notes, and snippets.

@rishi23root
Created September 14, 2021 18:50
Show Gist options
  • Save rishi23root/32a88bf23b134c9d768c2c16c7fe1b1a to your computer and use it in GitHub Desktop.
Save rishi23root/32a88bf23b134c9d768c2c16c7fe1b1a to your computer and use it in GitHub Desktop.
Image preLoader for react 'to show loading circle till the image loads'
import React, { useState, Suspense } from 'react';
import CircularProgress from '@material-ui/core/CircularProgress';
export default function Image({ src, alt }) {
const [isloaded, setIsloaded] = useState(false);
return (
<>
<img
className={isloaded ? "ImageItself " : "displayNone"}
src={src}
alt={alt || "Poster for the movie"}
onLoad={() => {
console.log(132)
setIsloaded(true)
}}
/>
{!isloaded && <CircularProgress color="secondary" />}
</>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment