3.0.1 • Published 20h ago
lesca-react-onload
Licence
—
Version
3.0.1
Deps
0
Size
11 kB
Vulns
0
Weekly
0
Why use it?
Progress and timing provided for loading and animation.
Live Demo
codesandbox.io
Installation
npm install lesca-react-onload --save
Usage
import OnloadProvider from 'lesca-react-onload';
export default function () {
const onStep = (e) => {
const {loaded, total} = e;
const percent = loaded / total * 100;
console.log(percent) // get loading percent
}
const onload = (e) => {
// all image loaded
}
return (
<OnloadProvider hideBeforeLoaded onStep={onStep} onload={onload}>
<img src='./myImage.jpg'>
<div style={{ backgroundImage: 'url(./myBackgroundImage.jpg)' }} />
</OnloadProvider>
);
};
Properties
| Properties | description | default |
|---|---|---|
| hideBeforeLoaded:boolean | hide DOM before loaded | true |
| onStep:function | call when each image loaded | |
| onload:function | call when all image loaded | |
| onStart:function | call when scan finished |
args
| args | description |
|---|---|
| loaded:number | target image loaded number |
| total:number | target image total number |
| index:number | target image load index |
| url:string | target url |
| failed:string[] | urls that failed to load so far |
Error handling
If an image fails to load, a console.warn is printed and the image is still counted in loaded, so progress always reaches 100% and onload still fires. Check failed in the result to see which urls failed. Identical urls are requested only once per load.
Features
- maintain if necessary