I have a React app which makes a call in useEffect
to my API which returns a list of URLs to use as imy image srcs.
I am using react-loader-spinner
to show a loading spinner component while my images load.
I have a loading
variable in useState
to determine whether the images are loading.
I can not figure out how to stop showing the loading spinner and show my images once they have all loaded.
Here is my code:
Photos.jsx
import React, { useState, useEffect, Fragment } from 'react'
import Loader from 'react-loader-spinner';
import { getAllImages } from '../../services/media.service';
import Photo from '../common/Photo';
const Photos = () => {
const [photos, setPhotos] = useState([]);
const [loading, setLoading] = useState(false);
useEffect(() => {
setLoading(true);
getAllImages()
.then(results => {
setPhotos(results.data)
console.log(results.data)
})
.catch(err =>{
console.log(err)
})
}, [])
const handleLoading = () => {
setLoading(false)
}
return (
<Fragment>
<div className="photos">
{ loading ?
<Fragment>
<Loader
height="100"
width="100"
/>
<div>Loading Joe's life...</div>
</Fragment>
:
photos.map((photo, index) => (
index !== photos.length - 1 ?
<Photo src={photo.src} key={photo.id} /> :
<Photo src={photo.src} key={photo.id} handleLoad={handleLoading}/>
))
}
</div>
</Fragment>
);
}
export default Photos;
Photo.jsx
import React from 'react'
import './Photo.css';
const Photo = (props) => {
return (
<div className="photo">
<img src={props.src} alt={props.alt} onLoad={props.handleLoad}/>
<div className="caption">
Photo caption
</div>
</div>
);
}
export default Photo;
I tried using onLoad
for my last item but it will never get called because loading
is never set back to false due to the spinner still being shown.
Some help on this would be greatly appreciated. Thanks
A workaround is to add the spinner class to the react container, and use the :empty pseudo class. The spinner will be visible, as long as nothing is rendered into the container (comments don't count). As soon as react renders something other than comment, the loader will disappear.
How Simple Progressive Image Loading is Achieved? Encapsulate the image in a container div. Load a tiny version of the image with the original image. Hide the original image till its loaded (Use the onload event for detection) and display the blurred version of the tiny version.
The reason why onLoad was never called, is because you never had the images in the DOM, so instead of conditionally rendering, conditionally set the display property to none & block.
Below is a simple example of how you could wait for all images to load.
Safe to assume the image with the largest file size would most likely be the last to load
Most certainly not!!, the time it takes for an image to load is not always down to size, caching, or server load can effect these.
const {useState, useEffect, useRef} = React;
const urls = [
"https://placeimg.com/100/100/any&rnd=" + Math.random(),
"https://placeimg.com/100/100/any&rnd=" + Math.random(),
"https://placeimg.com/100/100/any&rnd=" + Math.random()
];
function Test() {
const [loading, setLoading] = useState(true);
const counter = useRef(0);
const imageLoaded = () => {
counter.current += 1;
if (counter.current >= urls.length) {
setLoading(false);
}
}
return <React.Fragment>
<div style={{display: loading ? "block" : "none"}}>
Loading images,
</div>
<div style={{display: loading ? "none" : "block"}}>
{urls.map(url =>
<img
key={url}
src={url}
onLoad={imageLoaded}/>)}
</div>
</React.Fragment>;
}
ReactDOM.render(<React.Fragment>
<Test/>
</React.Fragment>, document.querySelector('#mount'));
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="mount"></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With