Gathering detailed insights and metrics for react-lazy
Gathering detailed insights and metrics for react-lazy
Gathering detailed insights and metrics for react-lazy
Gathering detailed insights and metrics for react-lazy
react-lazy-load
Simple lazy loading component built with react
react-lazy-load-image-component
React Component to lazy load images using a HOC to track window scroll position.
@types/react-lazy-load-image-component
TypeScript definitions for react-lazy-load-image-component
react-lazy-cache
A utility to lazily calculate and cache values in a react component based on props
npm install react-lazy
Typescript
Module System
Node Version
NPM Version
65.4
Supply Chain
94
Quality
75.3
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
467,960
Last Day
122
Last Week
741
Last Month
3,119
Last Year
32,598
MIT License
118 Stars
126 Commits
8 Forks
1 Watchers
30 Branches
1 Contributors
Updated on Dec 16, 2024
Minified
Minified + Gzipped
Latest Version
1.1.0
Package Id
react-lazy@1.1.0
Unpacked Size
57.13 kB
Size
11.71 kB
File Count
16
NPM Version
6.7.0
Node Version
10.15.0
Cumulative downloads
Total Downloads
Last Day
-25.6%
122
Compared to previous day
Last Week
-11.9%
741
Compared to previous week
Last Month
24.6%
3,119
Compared to previous month
Last Year
-27.5%
32,598
Compared to previous year
1
3
32
Lazy load your content without breaking the internet!
Supports universal rendering including disabled JavaScript by using noscript
elements that are also friendly to all
search engines. Uses modern IntersectionObserver API using the excellent
@researchgate/react-intersection-observer.
Also optionally supports displaying the content on IE8 and earlier by adding conditional comments to skip noscript
elements.
1npm install react-lazy 2 3import { Lazy } from 'react-lazy' 4// or 5import { LazyGroup } from 'react-lazy'
You also need to polyfill intersection-observer
! Use polyfill.io or require('intersection-observer')
. Check
Can I use for browser support status. Map
and Set
are also
required, but these are required by React as well.
react-lazy
?noscript
is done for youYou want to save your bandwidth and/or server load. As a side effect you may also gain some performance benefits on client side, especially on mobile devices. However the main benefit (and main purpose) for you should always be the reduction of bandwidth/server load.
Likely side effect of lazy loading is that user may see content flashing as it comes into view; sometimes with a lot of
delay as it depends on connectivity. You can make the experience less flickery by adding a transition when image is
loaded (a bit harder to develop) or by giving Lazy
a large cushion (500 pixels or more) to load image before it is
actually in the viewport. Using both strategies together is recommended. You can test the experience on your own site by
dropping mobile connection to slow 3G.
Chrome developer tools also has network throttling so you don't need to get yourself into a train to nowhere to test how well or poorly your site works in high latency conditions. However it is also recommended you do get yourself into a train to nowhere as it does good for your mind and soul to abandon the hectic although convenient city lifestyle every once in a while.
<Lazy />
1// curly brackets are required 2import { Lazy } from 'react-lazy' 3 4... 5 6 <Lazy component="a" href="/" className="image-link image-link--100px" ltIE9> 7 <img alt="My Lazy Loaded Image" className="image-link__image" src="my-lazy-loaded-image.png" /> 8 </Lazy>
1<!-- server render and render before component is in viewport --> 2<a href="/" class="image-link image-link--100px"> 3 <!--[if IE 9]><!--><noscript><!--<![endif]--> 4 <img alt="My Lazy Loaded Image" class="image-link__image" src="my-lazy-loaded-image.png" /> 5 <!--[if IE 9]><!--></noscript><!--<![endif]--> 6</a> 7 8<!-- client DOM after component is in viewport --> 9<a href="/" class="image-link image-link--100px"> 10 <img alt="My Lazy Loaded Image" class="image-link__image" src="my-lazy-loaded-image.png" /> 11</a>
There are two components: <Lazy />
and <LazyGroup />
.
Lazy provides basic functionality for lazy loading: it keeps render in noscript
element until it has come into
viewport and then simply swaps render. Everything inside the component is wrapped into noscript
. As the component
is quite simple and generic it doesn't support many other things that provide convenience; for example, with images you
have to write your own logic for handling onError
and onLoad
so that you can do things like trigger transitions as
images are loaded, or change what to render instead of the image if loading the image fails.
LazyGroup extends Lazy
functionality by wrapping only specified component types inside noscript
. So only the
specified components like img
or iframe
elements are wrapped to noscript
. Other elements are simply rendered
as-is.
The wrappable components (img
s and iframe
s by default) are also always wrapped inside another component. This custom
component will receive information on whether loading the img
or iframe
has succeeded or failed, thus allowing a
single place to control lifecycles as images or other content is loaded.
These features are supported by both <Lazy />
and <LazyGroup />
.
viewport
(= root
option)cushion
(= rootMargin
option)threshold
These props work like you would expect them to work with IntersectionObserver.
clientOnly
propDisables noscript
element rendering, instead rendering no HTML for the contents in server side. This gives behavior
similar to most other lazy loaders, which is why it is not enabled by default in react-lazy
.
ltIE9
propRenders Internet Explorer 8 friendly syntax by adding conditional comments around noscript
, effectively hiding
existance of the tag from IE8 and earlier. This allows for minimal legacy browser support, since it is highly unlikely
anyone writes their JS to execute on IE8 anymore.
Essentially this feature allows to render a visually non-broken page to users of legacy browsers, making it possible to give minimally acceptable user experience to users of browsers that should be dead.
This means there is no lazy rendering on legacy browsers, images load immediately.
This prop has no effect if clientOnly
is enabled.
onLoad
Lazy
triggers after removing noscript
element.LazyGroup
triggers after all wrapped child components onLoad
or onError
events have triggered.1<Lazy onLoad={yourCustomFunction}>...</Lazy>
onViewport
Triggers before removing noscript
elements. Given function receives IntersectionObserver event object.
visible
Allows you to manually tell if the element is actually visible to the user or not.
<LazyGroup />
Lazy
works fine with single images, but sometimes you may want to have slightly more control or better performance
when you know multiple images load at the same time (for example, a row of thumbnails). In this case it makes no sense
to check each individual image's position in viewport when checking for just the container component will be good enough
— and also less for a browser to execute.
You can also use Lazy
for multiple images, but there are some practical limitations such as the fact that everything
inside Lazy
is within noscript
element, thus there is nothing rendered inside. LazyGroup
solves this issue by
rendering noscript
only around specific wrapped elements (img
and iframe
by default). Also, further control is
given with childWrapper
component that will receive a set of props to make life easier.
Use cases:
childWrapper
instead of writing custom logic.1// curly brackets are required 2import { LazyGroup } from 'react-lazy' 3 4function ImageContainer({ childProps, children, isFailed, isLoaded, ...props }) { 5 return ( 6 // usually the other props include `dangerouslySetInnerHtml` when rendering `noscript` element 7 <div {...props}> 8 {isFailed ? 'The image did not load :( ' + childProps.src : children} 9 </div> 10 ) 11} 12 13... 14 15 <LazyGroup component="ul" className="image-list" childWrapper={ImageContainer}> 16 {this.props.images.map((image, index) => 17 <li key={index} className="image-list__item"> 18 <img {...image} /> 19 </li> 20 )} 21 </LazyGroup>
childWrapper
lifecycleLazyGroup
container is in viewport in client childWrapper
will receive
dangerouslySetInnerHtml
prop (thus rendering noscript
element that contains the lazily loaded content).isFailed
and isLoaded
are false. childProps
also become available.isFailed
is set to true when img
's or iframe
's onError
event triggers. You can use childProps
to decide
what to render.isLoaded
is set to true when img
's or iframe
's onLoad
event triggers.childrenToWrap
Use this array to decide which components are wrapped by childWrapper
. Default value: ['iframe', 'img']
Note! The components must support onError
and onLoad
events as these are used to detect loading.
You can see these components via React developer tools, but as of 1.0.2 they have not been exposed.
DefaultWrapper
This is the childWrapper
used to render LazyGroup
's wrapped childs if no custom wrapper is given. The wrapper is a
simple div with a className
of react-lazy-wrapper
. BEM convention is used to tell about the lifecycle:
react-lazy-wrapper--placeholder
is set on server render and client render before LazyGroup
is in viewport.react-lazy-wrapper--loading
is set once LazyGroup
is in viewport.react-lazy-wrapper--failed
is set if lazy loaded component's onError
event has triggered.react-lazy-wrapper--loaded
is set if lazy loaded component's onLoad
event has triggered.LazyChild
This is the component used by LazyGroup
to handle rendering of the wrapped child components. It manages the onLoad
/
onError
handling. It takes two props: callback
and wrapper
. callback
is called by LazyChild
once loading
result has been resolved. wrapper
is the component rendered around wrapped child element.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 0/20 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
126 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-02-10
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More