Installations
npm install react-lazy
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
10.15.0
NPM Version
6.7.0
Score
65.4
Supply Chain
94
Quality
75.3
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
Download Statistics
Total Downloads
467,960
Last Day
122
Last Week
741
Last Month
3,119
Last Year
32,598
GitHub Statistics
MIT License
118 Stars
126 Commits
8 Forks
1 Watchers
30 Branches
1 Contributors
Updated on Dec 16, 2024
Bundle Size
12.26 kB
Minified
3.56 kB
Minified + Gzipped
Package Meta Information
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
Total Downloads
Cumulative downloads
Total Downloads
467,960
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
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Peer Dependencies
3
Dev Dependencies
32
react-lazy
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.
Why react-lazy
?
- Minimalistic and performant implementation with less dependencies than other solutions
- You can choose between ease-of-use (LazyGroup) and do-it-yourself (Lazy)
- The hard part of handling
noscript
is done for you
Why lazy load content such as images?
You 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.
Usage: <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>
Component introduction
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.
Shared features
These features are supported by both <Lazy />
and <LazyGroup />
.
IntersectionObserver props
viewport
(=root
option)cushion
(=rootMargin
option)threshold
These props work like you would expect them to work with IntersectionObserver.
clientOnly
prop
Disables 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
prop
Renders 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
- On
Lazy
triggers after removingnoscript
element. - On
LazyGroup
triggers after all wrapped child componentsonLoad
oronError
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:
- You want all contained images/iframes to be transitioned at the exact same time after everything is loaded.
- You want to use the abstraction provided by
childWrapper
instead of writing custom logic. - You want to have slightly better performance by only checking the container element's location relative to the view.
Usage
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
lifecycle
- On server side render and before the
LazyGroup
container is in viewport in clientchildWrapper
will receivedangerouslySetInnerHtml
prop (thus renderingnoscript
element that contains the lazily loaded content). - After coming into viewport
isFailed
andisLoaded
are false.childProps
also become available. isFailed
is set to true whenimg
's oriframe
'sonError
event triggers. You can usechildProps
to decide what to render.isLoaded
is set to true whenimg
's oriframe
'sonLoad
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.
Other components
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 beforeLazyGroup
is in viewport.react-lazy-wrapper--loading
is set onceLazyGroup
is in viewport.react-lazy-wrapper--failed
is set if lazy loaded component'sonError
event has triggered.react-lazy-wrapper--loaded
is set if lazy loaded component'sonLoad
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
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
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
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'v1'
- Warn: branch protection not enabled for branch 'gh-pages'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 10 are checked with a SAST tool
Reason
126 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-6chw-6frg-f759
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-whgm-jr23-g3j9
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-fwr7-v2mv-hh25
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx
- Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-x9w5-v3q2-3rhw
- Warn: Project is vulnerable to: GHSA-257v-vj4p-3w2h
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-p28h-cc7q-c4fg
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-ff7x-qrg7-qggm
- Warn: Project is vulnerable to: GHSA-vh7m-p724-62c2
- Warn: Project is vulnerable to: GHSA-r9p9-mrjm-926w
- Warn: Project is vulnerable to: GHSA-434g-2637-qmqr
- Warn: Project is vulnerable to: GHSA-49q7-c7j4-3p7m
- Warn: Project is vulnerable to: GHSA-977x-g7h5-7qgw
- Warn: Project is vulnerable to: GHSA-f7q4-pwc6-w24p
- Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747
- Warn: Project is vulnerable to: GHSA-vjh7-7g9h-fjfh
- Warn: Project is vulnerable to: GHSA-4gmj-3p3h-gm8h
- Warn: Project is vulnerable to: GHSA-6h5x-7c5m-7cr7
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-pfrx-2q88-qq97
- Warn: Project is vulnerable to: GHSA-w457-6q6x-cgp9
- Warn: Project is vulnerable to: GHSA-62gr-4qp9-h98f
- Warn: Project is vulnerable to: GHSA-f52g-6jhx-586p
- Warn: Project is vulnerable to: GHSA-2cf5-4w76-r9qv
- Warn: Project is vulnerable to: GHSA-3cqr-58rm-57f8
- Warn: Project is vulnerable to: GHSA-g9r4-xpmj-mj65
- Warn: Project is vulnerable to: GHSA-q2c6-c6pm-g3gh
- Warn: Project is vulnerable to: GHSA-765h-qjxv-5f44
- Warn: Project is vulnerable to: GHSA-f2jv-r9rf-7988
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-pfq8-rq6v-vf5m
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-7r28-3m3f-r2pr
- Warn: Project is vulnerable to: GHSA-r8j5-h5cx-65gg
- Warn: Project is vulnerable to: GHSA-2pr6-76vf-7546
- Warn: Project is vulnerable to: GHSA-8j8c-7jfh-h6hx
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488
- Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-xf5p-87ch-gxw2
- Warn: Project is vulnerable to: GHSA-5v2h-r2cx-5xgj
- Warn: Project is vulnerable to: GHSA-rrrm-qjm4-v8hf
- Warn: Project is vulnerable to: GHSA-4xcv-9jjx-gfj3
- Warn: Project is vulnerable to: GHSA-7wpw-2hjm-89gp
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-fhjf-83wg-r2j9
- Warn: Project is vulnerable to: GHSA-8hfj-j24r-96c4
- Warn: Project is vulnerable to: GHSA-wc69-rhjr-hc9g
- Warn: Project is vulnerable to: GHSA-r683-j2x4-v87g
- Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p
- Warn: Project is vulnerable to: GHSA-rp65-9cf3-cjxr
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-rhx6-c78j-4q9w
- Warn: Project is vulnerable to: GHSA-566m-qj78-rww5
- Warn: Project is vulnerable to: GHSA-7fh5-64p2-3v2j
- Warn: Project is vulnerable to: GHSA-wvhm-4hhf-97x9
- Warn: Project is vulnerable to: GHSA-h4hr-7fg3-h35w
- Warn: Project is vulnerable to: GHSA-gj77-59wh-66hg
- Warn: Project is vulnerable to: GHSA-hqhp-5p83-hx96
- Warn: Project is vulnerable to: GHSA-3949-f494-cm99
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-5q6m-3h65-w53x
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-44c6-4v22-4mhx
- Warn: Project is vulnerable to: GHSA-4x5v-gmq8-25ch
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-h9rv-jmmf-4pgx
- Warn: Project is vulnerable to: GHSA-hxcc-f52p-wc94
- Warn: Project is vulnerable to: GHSA-76p7-773f-r4q5
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-4g88-fppr-53pp
- Warn: Project is vulnerable to: GHSA-4jqc-8m5r-9rpr
- Warn: Project is vulnerable to: GHSA-g4rg-993r-mgx7
- Warn: Project is vulnerable to: GHSA-4rq4-32rv-6wp6
- Warn: Project is vulnerable to: GHSA-64g7-mvw6-v9qj
- Warn: Project is vulnerable to: GHSA-vx3p-948g-6vhq
- Warn: Project is vulnerable to: GHSA-7xcx-6wjh-7xp2
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-7p7h-4mm5-852v
- Warn: Project is vulnerable to: GHSA-38fc-wpqx-33j7
- Warn: Project is vulnerable to: GHSA-662x-fhqg-9p8v
- Warn: Project is vulnerable to: GHSA-394c-5j6w-4xmx
- Warn: Project is vulnerable to: GHSA-78cj-fxph-m83p
- Warn: Project is vulnerable to: GHSA-fhg7-m89q-25r3
- Warn: Project is vulnerable to: GHSA-46c4-8wrp-j99v
- Warn: Project is vulnerable to: GHSA-9m6j-fcg5-2442
- Warn: Project is vulnerable to: GHSA-hh27-ffr2-f2jc
- Warn: Project is vulnerable to: GHSA-rqff-837h-mm52
- Warn: Project is vulnerable to: GHSA-8v38-pw62-9cw2
- Warn: Project is vulnerable to: GHSA-hgjh-723h-mx2j
- Warn: Project is vulnerable to: GHSA-jf5r-8hm2-f872
- Warn: Project is vulnerable to: GHSA-wr3j-pwj9-hqq6
- Warn: Project is vulnerable to: GHSA-g78m-2chm-r7qv
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
Score
1.7
/10
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 MoreOther packages similar to 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