+0
日增长
5,885
stars
482
forks
48
watchers
140
prs
277
issues
0
releases
README
Note
This project is now currently maintained by
@ameerthehacker, please reach out to him on any issues or help.
----
react-lazyload
Lazyload your Components, Images or anything matters the performance.
Demo
Why it's better
- Take performance in mind, only 2 event listeners for all lazy-loaded components
- Support both one-time lazy load and continuous lazy load mode
- scroll / resize event handler is throttled so you won't suffer frequent update, you can switch to debounce mode too
- Decorator supported
- Server Side Rendering friendly
- Thoroughly tested
Installation
2.0.0 is finally out, read Upgrade Guide, it's almost painless to upgrade!
3.0.0 fixes the findDomNode warning through usage of React ref, and the following are the changes you need to be aware of
Now we have an extra div wrapping the lazy loaded component for the React ref to work
We can understand that it is an extra DOM node, and we are working to optimize that if possible
It might break your UI or snapshot tests based on your usage
To customize the styling to the extra div please refer here
Found any other problem, please feel free to leave a comment over here
$ npm install --save react-lazyload
Usage
javascript
import React from 'react';
import ReactDOM from 'react-dom';
import LazyLoad from 'react-lazyload';
import MyComponent from './MyComponent';
const App = () = {
return (
/
Lazy loading images is supported out of box,
no extra config needed, set height for better
experience
/
/ Once this component is loaded, LazyLoad will
not care about it anymore, set this to true
if you're concerned about improving performance /
/ This component will be loaded when it's top
edge is 100px from viewport. It's useful to
make user ignorant about lazy load effect. /
);
};
ReactDOM.render(, document.body);
If you want to have your component lazyloaded by default, try this handy decorator:
javascript
import { lazyload } from 'react-lazyload';
@lazyload({
height: 200,
once: true,
offset: 100
})
class MyComponent extends React.Component {
render() {
return this component is lazyloaded by default!;
}
}
Special Tips
You should be aware that your component will only be mounted when it's visible in viewport, before that a placeholder will be rendered.
So you can safely send request in your component's componentDidMount without worrying about performance loss or add some pretty entering effects, see this demo for more detail.
Props
children
Type: Node Default: undefined
NOTICE
Only one child is allowed tTop issues
#310[QUESTION]: lazyload-wrapper class33 comments · 2020-06-18#380react 18 support17 comments · 2022-04-11#109Support for search bots17 comments · 2017-07-19#175Items do not render when they are scrolled into view15 comments · 2018-04-08#323How to use react-lazyload horizontally.. please provide example14 comments · 2020-08-20#172overflow={true} still can`t be shown14 comments · 2018-03-27#129visibility detection12 comments · 2017-10-17#221Please add Changelog11 comments · 2019-01-30