React-load-lazily is a Tiny library that allows you to load any component or HTML element only when the user scrolls to its position on the viewport.
- ✅ Uses IntersectionObserver API natively present in the browser.
- 📦 Lightweight ~ 1.5KB minified & gzipped (7xxKb)
- 🚀 Easy to use
npm install react-load-lazily --save
yarn add react-load-lazily
import LazyLoad from 'react-load-lazily'<LazyLoad>
<MyComponent>
</LazyLoad><LazyLoad component={<MyComponent>}/>Default: 0.1
The threshold property can be used to specify the percentage of the element that must be visible before it is loaded, with the
defaultthreshold set at0.1(or 10%).
threshold={0.5} Load the component when 50% of it is visible.
threshold={1} Load the component when 100% of it is visible
Note
When theheightandwidthproperties are not specified, the default is set to none, meaning that the component will be loaded as soon as even one pixel of the element is visible on the viewport.
<LazyLoad threshold={0.5} height="500px" width="100vw">
<MyComponent>
</LazyLoad><LazyLoad threshold={0.5} styles={{ height:'100px' , width:'500px' }} className="h-12 w-full" id="myComponent">
<MyComponent>
</LazyLoad><LazyLoad onVisible={()=> {
window.alert('visible')
}}
>
<MyComponent>
</LazyLoad><LazyLoad onClick={...} onHover={...}>
<MyComponent>
</LazyLoad>👤 Hamsaraj
- Github: @hamsarajpitani
- LinkedIn: @pitani-hamsaraj
Give a ⭐️ if this project helped you!