react-lazy-props
LazyLoad media elements with react-lazy-props
and IntersectionObserver
Installation
npm i react-lazy-props
Usage
Wrap the elements you want to lazyload with LazyProps
component
It will unload media before components were mounted and will load them only when they are observed.
Example:
;...return <LazyProps> <div> <h2 style=backgroundImage: `url("")`>Title</h2> <p>Some Images</p> <img src="example.jpg" srcSet="example2x.jpg 2x" "example3x.jpg 3x" /> <video src="https://www.example.com/video.ogg" /> </div> </LazyProps>;
Unloading process:
LazyProps
renders its child components with props and styles replaced according to the map:
src
todata-src
srcSet
todata-srcset
backgroundImage
todata-bg-src
HTML Output (Unloaded):
Title Some Images
Loading process:
LazyProps
component observes NodeList with Unloaded Prop Keys using IntersectionObserverAPI
.
As soon as at least 1/10 of Node element is intersected, element props will gain thier initial key.
HTML Output (Loaded):
Title Some Images
Component Props
children (undefined)
NOTE! Always wrap child components as LazyProps
component returns its children unwrapped.
onElementLoad (function)
Arguments:
element
- Node Object
Function will be invoked when one of the child elements is intersected.
;... { elementclassList; }}...render return <LazyProps onElementLoad= this>...
onUnloadProps (function)
Arguments:
props
- ObjectcomponentType
- String
Function will be invoked after component props were unloaded, this way you will be able to customize unloaded props of the element, for example setup a placeholder src to images:
;... { ifcomponentType === "img" propssrc = thisstateplaceholder; } return props;}...render return <LazyProps onUnloadProps= this>...
NOTE! The function must always return props object.
unloaded (boolean)
Default: false
If set to true elements wont be unloaded, but observer still will be there, this approach will be more efficient because we will not have to loop through child elements and components unloading their props.
The differecne is that you will have to set lazy props yourself:
;...return <LazyProps unloaded=true> <div> <h2 data-bg-src=backgroundImage>Title</h2> <p>Some Images</p> <img data-src="example.jpg" /> <video data-src="https://www.example.com/video.ogg" /> </div> </LazyProps>;
IntersectionObserver polyfill
Since this package is observing elements with IntersectionObserver
, you may want old browsers to support it with IntersectionObserver polyfill