GithubHelp home page GithubHelp logo

loktar00 / react-lazy-load Goto Github PK

View Code? Open in Web Editor NEW
975.0 13.0 165.0 606 KB

React component that renders children elements when they enter the viewport.

License: MIT License

JavaScript 45.87% TypeScript 54.13%

react-lazy-load's Introduction

React Lazy Load Component

React Lazy Load is an easy-to-use React component which helps you defer loading content in predictable way. It's fast, You can also use component inside scrolling container, such as div with scrollbar. It will be found automatically. Check out an example.

build status npm downloads

4.0 Update

Converted to work with React 18, and updated to use the Intersection Observer API.

Breaking changes

  • No more debunce, or throttle options as they aren't needed
  • Removed individual offset props, offset can be used just like css margin, eg. offset={'0px 10px 200px 0px'} or offset={100}

Improvements

Since we're now using the intersection observer we don't need to watch a user scroll, more importantly we don't have to manually calculate if a user can see the element or not. Also took this oppourtunity to convert over to TS.

Installation

React Lazy Load requires React 17 or later.

npm i -S react-lazy-load

Examples

Examples

Basic Usage

import React from 'react';
import LazyLoad from 'react-lazy-load';

const MyComponent = () => (
  <div>
    <LazyLoad height={762}>
      <img src='http://apod.nasa.gov/apod/image/1502/HDR_MVMQ20Feb2015ouellet1024.jpg' />
    </LazyLoad>
  </div>
)

Loading the image 300px prior to scroll

const MyComponent = () => (
  <div>
    <LazyLoad height={762} offset={300}>
      <img src='http://apod.nasa.gov/apod/image/1502/HDR_MVMQ20Feb2015ouellet1024.jpg' />
    </LazyLoad>
  </div>
)

Loading image only when 95% of it is in the viewport. note a width must be specified.

const MyComponent = () => (
  <div>
    <LazyLoad height={762} width={400} threshold={0.95}>
      <img src='http://apod.nasa.gov/apod/image/1502/HDR_MVMQ20Feb2015ouellet1024.jpg' />
    </LazyLoad>
  </div>
)

Performing a side effect once your image is loaded

const MyComponent = () => (
  <div>
    <LazyLoad height={762} width={400} threshold={0.95} onContentVisible={() => {console.log('loaded!')}}>
      <img src='http://apod.nasa.gov/apod/image/1502/HDR_MVMQ20Feb2015ouellet1024.jpg' />
    </LazyLoad>
  </div>
)

Example controlling element transition

There are two primary classes you an use to control the component LazyLoad and is-visible

/* Example of how to fade our content in */

.LazyLoad {
  opacity: 0;
  transition: all 1s ease-in-out;
}

.is-visible {
  opacity: 1;
}

Props

offset

Type: Number|String Default: 0

The offset option allows you to specify how far below, above, to the left, and to the right of the viewport you want to begin displaying your content. If you specify 0, your content will be displayed as soon as it is visible in the viewport, if you want to load 1000px below or above the viewport, use 1000.

threshold

Type: Number Default: 0

This threshold option allows you to specify how much of the element must be shown on the screen prior to loading. This requires a width and height to be set on the <LazyLoad> component in order for the browser to calcualte the viewable area.

height

Type: String|Number

The height option allows you to set the element's height even when it has no content.

width

Type: String|Number

The width option allows you to set the element's width even when it has no content.

onContentVisible

Type Function

A callback function to execute when the content appears on the screen.

Building LazyLoad

npm run build

Running examples

cd examples/basic
npm run dev

react-lazy-load's People

Contributors

7rulnik avatar amillward avatar benvinegar avatar brucewpaul avatar duvilliera avatar fabiosantoscode avatar ghesericsu avatar gpbl avatar hon2a avatar kjell avatar lencioni avatar loktar00 avatar mattgoucher avatar maxkueng avatar mushishi78 avatar nickpalmer avatar palpants avatar patrickleet avatar polydecay avatar reintroducing avatar sergeylaptev avatar wopian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-lazy-load's Issues

Error during installation `The package react does not satisfy its siblings' peerDependencies requirements`

I get the following error

npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No README data
npm WARN package.json [email protected] path is also the name of a node core module.
npm ERR! peerinvalid The package react does not satisfy its siblings' peerDependencies requirements!
npm ERR! peerinvalid Peer [email protected] wants [email protected]
npm ERR! peerinvalid Peer [email protected] wants react@>=0.12.*
npm ERR! peerinvalid Peer [email protected] wants [email protected]
npm ERR! peerinvalid Peer [email protected] wants react@>=0.12.2
npm ERR! peerinvalid Peer [email protected] wants react@^0.12.2
npm ERR! peerinvalid Peer [email protected] wants react@>=0.13
npm ERR! peerinvalid Peer [email protected] wants [email protected]

npm ERR! System Linux 3.2.0-82-generic-pae
npm ERR! command "/usr/bin/node" "/usr/bin/npm" "install" "--save" "react-lazy-load"
npm ERR! cwd /home/user/workspace/floormap
npm ERR! node -v v0.10.37
npm ERR! npm -v 1.4.28
npm ERR! code EPEERINVALID
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /home/user/workspace/floormap/npm-debug.log
npm ERR! not ok code 0

ES6 breaks existing imports

I used to be able to do this:

LazyLoad = require('react-lazy-load')

but now need to do this:

LazyLoad = require('react-lazy-load').default

in Webpack + Javascript. Perhaps a warning in the README?

Warning: React.createElement: type should not be null

I have a very simple component that uses LazyLoad a bit like this:

var LazyLoad = require('react-lazy-load')
var View = React.createClass({
    render: function() {
        return (
            <LazyLoad>
                <img src={…}/>
            </LazyLoad>
        )
})

But when displayed by browser, I get this error in the console:

Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components). Check the render method of `View`.
Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. Check the render method of `View`.

What am I doing wrong? I am using 2.0.2 on Node 5.4.1.

Listen for animationend event

I am testing the 3rd version of the component. Our carousel component has a number of slides which loaded immediately in 2nd version when entered into viewport.

furnishings the art of furniture from 16th to 20th century auction auctionata

But with 3rd version the logic of lazy component loads only images which are visible so when we click on next slide button the images are not loaded until we start scrolling.

furnishings the art of furniture from 16th to 20th century auction auctionata 2

There multiple approaches to handle the issue. One would be listening on animation events, another to expose a callback we could call to trigger loading logic evaluation.

What would be the suggested approach in my case?

Add onContentNotVisibleAnymore (or something like that)

Hello,

I'm using your react component and for my use-case I miss a simple feature,
I want to call a function when an element was visible, but not anymore.

I'm using your stuff for a news reader, when i scroll to read a news, and I arrive to the bottom of it, (means I've read it) I want to call a function to set it as Read.

What do you think about this feature ?

Thanks

Support for Image Not Found Errors

Thanks for such a light weight component. I wrap all my images in it. Since all my images are wrapped in this component, it'd be nice if I could tweak it somehow to handle Image Not Found errors(404). So if an image couldn't load, it would be handled gracefully. Any ideas how we can go about that?

findDOMNode called on an unmounted component

I have integrated react-lazy-load, however, I am seeing the following problem when switching routes in our app.

Uncaught Error: findDOMNode was called on an unmounted component.
at invariant (invariant.js:38)
at findDOMNode (findDOMNode.js:52)
at LazyLoad.lazyLoadHandler (LazyLoad.js:121)
at invokeFunc (index.js:160)
at trailingEdge (index.js:207)
at timerExpired (index.js:195)
at _super.buglog (buglog.js:82)

LazyLoad.js:121 is the first call to findDOMNode in the handler function. In debugging this I am seeing cancel() being called on the handler and the events being unregistered however, it is still being called.

npm ls tells me:

├─┬ [email protected]
│ ├── [email protected]
│ └── [email protected]

Performance is extremely bad

When many instances of this component are on the page, the page becomes noticeably less performant. Performance could be made better by (1) limiting the number of scroll/resize event handlers are attached to window to just 1 and (2) throttling the event handler so that it's only executed once per time interval (250ms is a good place to start).

How to deal with floating elements?

I am wondering if I can use this npm with floating elements.

I use floating cards, and especially on mobile, I would really like to lazy load this list.

Is this possible?

Not responsive

With the requirement of a fixed height setting, this plugin fails to work inside modern apps where responsive layouts are a standard requirement.

does not show images when root component re-renders

When children component is already rendered, but root component re-renders (for instance filtering table rows) and LazyLoad component becomes visible, it does not check if it is inViewport or not, so images does not show up, until you scroll or resize (sometimes when filtering your scroll bar disappears, as content is really small 😄)

easy fix, which I tried, would be calling lazyLoadHandler inside componentWillReceiveProps function.

[email protected] requires a peer of react-dom@^0.14.0 || ^15.0.0-0 but none was installed.

My React-native version
react-native-cli: 1.0.0
react-native: 0.27.2

My Package.json file

"dependencies": {
"@remobile/react-native-action-sheet": "^1.0.1",
"alt": "^0.18.5",
"lodash": "^3.10.0",
"react": "^15.1.0",
"react-lazy-load": "^3.0.10",
"react-native": "0.27.2",
"react-native-android-share": "0.0.4",
"react-native-bottom-sheet": "^0.1.1",
"react-native-button": "^1.6.0",
"react-native-custom-action-sheet": "0.0.11",
"react-native-drawer": "^2.2.3",
"react-native-gifted-spinner": "0.0.4",
"react-native-scrollview-lazyload": "^1.0.5",
"react-native-share": "^1.0.13",
"react-native-storage": "^0.1.2",
"react-native-swiper2": "^2.0.7",
"react-native-viewpager": "^0.2.11",
"superagent": "^2.1.0"
}

Avoid wrapper?

Do we really need this wrapper?
It breaks style-specific items on my list and I don't want to override those styles to lazy-loader's wrapper :\

Why don't just render actual child node, as react-lazyload does?

traverseParentPath(...): Cannot traverse from and to the same ID, ``

This error happens with CRA 1.0.7 and code in current master, but npm version "react-lazy-load": "^3.0.12" works fine

Here is example of code https://github.com/stereobooster/react-lazy-load/tree/cra-master-bug

invariant.js:39 Uncaught Error: traverseParentPath(...): Cannot traverse from and to the same ID, ``.
    at invariant (invariant.js:39)
    at traverseParentPath (ReactInstanceHandles.js:157)
    at Object.traverseAncestors (ReactInstanceHandles.js:285)
    at findDeepestCachedAncestor (ReactMount.js:234)
    at Object.findComponentRoot (ReactMount.js:708)
    at Object.findReactNodeByID (ReactMount.js:679)
    at Object.getNodeFromInstance (ReactMount.js:180)
    at findDOMNode (findDOMNode.js:43)
    at LazyLoad.getEventNode (LazyLoad.jsx:64)
    at LazyLoad.componentDidMount (LazyLoad.jsx:32)
    at ReactCompositeComponent.js:265
    at measureLifeCyclePerf (ReactCompositeComponent.js:75)
    at ReactCompositeComponent.js:264
    at CallbackQueue.notifyAll (CallbackQueue.js:76)
    at ReactReconcileTransaction.close (ReactReconcileTransaction.js:80)
    at ReactReconcileTransaction.closeAll (Transaction.js:206)
    at ReactReconcileTransaction.perform (Transaction.js:153)
    at batchedMountComponentIntoNode (ReactMount.js:126)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:140)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
    at Object.batchedUpdates (ReactUpdates.js:97)
    at Object._renderNewRootComponent (ReactMount.js:320)
    at Object._renderSubtreeIntoContainer (ReactMount.js:401)
    at Object.render (ReactMount.js:422)
    at Object.<anonymous> (index.js:6)
    at __webpack_require__ (bootstrap 45fac5b…:659)
    at fn (bootstrap 45fac5b…:85)
    at Object.<anonymous> (fetch.js:461)
    at __webpack_require__ (bootstrap 45fac5b…:659)
    at validateFormat (bootstrap 45fac5b…:708)
    at bundle.js:712

Does not respond to visibility change

I am using TabbedArea/TabPane from react-bootstrap. And when I switch tab, the classnames of panels change and therefore styles change, including visibility. I wonder is there a more elegant way than manually firing a scroll/resize event to window.

Not working on touch devices

Great component! It works perfect on web, but on touch devices doesn't load anything. It leaves the lazy-load sections blank, like if it wasn't detecting any scroll.

Any ideas? I tried running the example to check it on touch devices as well, but is throwing an error, already submitted in #93

Thanks!

Placeholder images

Is their a way we can keep a placeholder(text / image / gif) before the element appears on DOM
Ex: We have few set of images in list and we have to display a loader before the image appears.

Uncaught Error: getNodeFromInstance: Invalid argument.

I am using LazyLoad 3.0.10 in combination with react 15.2.0 amd react-dom 15.2.1

when openening the compontent the following error gets thrown:
Uncaught Error: getNodeFromInstance: Invalid argument.

screen shot 2016-08-17 at 16 05 35

this is a fragment of the code:

import LazyLoad from 'react-lazy-load';
import React from 'react';

    getGameMedia()
    {
        return(
            <div className="games_list_item_game_media">
                <LazyLoad height={300} offsetTop={200}>
                    <img src={this.props.data.tutorialThumbnail} className="width100" alt="" />
                </LazyLoad>
            </div>
        );
    }

Incorrect instructions for example README.md

You currently have the following for https://github.com/loktar00/react-lazy-load/blob/master/examples/basic/README.md

git clone https://github.com/loktar00/react-lazy-load.git

cd react-lazy-load/examples/basic
npm install
npm start

open http://localhost:3000/

This causes the following output for my machine :

npm start

> [email protected] start /Users/blakedietz/react-lazy-load/examples/basic
> node -r 'babel-core/register' ./initializers/server/index.js

/Users/blakedietz/react-lazy-load/examples/basic/node_modules/babel-core/lib/transformation/file/options/option-manager.js:372
          throw new Error("Couldn't find preset " + JSON.stringify(val) + " relative to directory " + JSON.stringify(dirname));
          ^

Error: Couldn't find preset "es2015" relative to directory "/Users/blakedietz/react-lazy-load"
    at /Users/blakedietz/react-lazy-load/examples/basic/node_modules/babel-core/lib/transformation/file/options/option-manager.js:372:17
    at Array.map (native)
    at OptionManager.resolvePresets (/Users/blakedietz/react-lazy-load/examples/basic/node_modules/babel-core/lib/transformation/file/options/option-manager.js:364:20)
    at OptionManager.mergePresets (/Users/blakedietz/react-lazy-load/examples/basic/node_modules/babel-core/lib/transformation/file/options/option-manager.js:348:10)
    at OptionManager.mergeOptions (/Users/blakedietz/react-lazy-load/examples/basic/node_modules/babel-core/lib/transformation/file/options/option-manager.js:307:14)
    at OptionManager.addConfig (/Users/blakedietz/react-lazy-load/examples/basic/node_modules/babel-core/lib/transformation/file/options/option-manager.js:207:10)
    at OptionManager.findConfigs (/Users/blakedietz/react-lazy-load/examples/basic/node_modules/babel-core/lib/transformation/file/options/option-manager.js:413:16)
    at OptionManager.init (/Users/blakedietz/react-lazy-load/examples/basic/node_modules/babel-core/lib/transformation/file/options/option-manager.js:461:12)
    at compile (/Users/blakedietz/react-lazy-load/examples/basic/node_modules/babel-register/lib/node.js:81:45)
    at loader (/Users/blakedietz/react-lazy-load/examples/basic/node_modules/babel-register/lib/node.js:126:14)

when instead it should be

git clone https://github.com/loktar00/react-lazy-load.git

cd react-lazy-load
npm install
cd ./examples/basic
npm install
npm start

open http://localhost:3000/

Dynamic loading of images results in blank images until scroll

When additional images are put on the page dynamically, the new images that are visible in the viewport are not loaded until a scroll event happens.

It would be nice to be able to trigger the "check for images in viewport" functionality manually.

Debounce events

Not 100% sure if this is needed, but its at least worth looking into.

Apply lazyload on state change as well

Hi :-)

Thanks a lot for your plugin. I use it to wrap images and it works very well on scroll.
Although, I have a list of content within a faceted search page and face an issue here.

When a facet is clicked, the content list gets filtered and some elements which meant to be outside of the viewport become above the fold but the lazyload is not triggered. For a good reason: scroll has not happened nor a window resize.

Do you think of another mechanism to trigger a lazyload in case of state change?

Thanks :-)

Here is a demo:

  1. scrolling works fine
  2. click on a filter
  3. nothing happens
  4. pictures appear after a brief moment after scroll

freebird-lazyload

Enable travis-ci

If you want to re-enable travis-ci for this repository, I can help with the .travis.yml stuff

Cannot Do Unit Test

I am getting following error when i do the test.

ReferenceError: HTMLElement is not defined
at scrollParent (node_modules/react-lazy-load/lib/utils/parentScroll.js:12:28)
at LazyLoad.getEventNode (node_modules/react-lazy-load/lib/LazyLoad.js:91:14)
at LazyLoad.componentDidMount (node_modules/react-lazy-load/lib/LazyLoad.js:56:28)
at [object Object].invokeComponentDidMountWithTimer (node_modules/react/lib/ReactCompositeComponent.js:60:18)

as soon as i remove lazy-load component test run green

Republish to NPM

Hi there,

Your latest commit (requiring react) is not updated in NPM.

Thanks!

Use requestAnimationFrame

Putting functions as the scroll handler/resize handler blocks scrolling while they execute, so it's preferable to use requestAnimationFrame for the handlers.

shouldComponentUpdate returns true when the item is visible

Hey there!
Im trying to understand whats the reason for the shouldcomponentupdate logic that you are applying.

  shouldComponentUpdate(_nextProps, nextState) {
    return nextState.visible;
  }

If the current state is visible and the next one is also visible then why should i update the component?
i would think that the wanted behavior should be:

  shouldComponentUpdate(_nextProps, nextState) {
    return !this.state.visible && nextState.visible;
  }

What am i missing here?
Thanks!

Only add one actual scroll handler to window

It's better to emulate multiple scroll handlers with one aggregation (put all the handlers in an array). This could be added as a static property and managed pretty easily. It's also possible to make optimizations from there (like keeping the elements in sorted order and skipping the rest of the array if you know you haven't gone down that far yet).

Not working on mobile view

@sergeylaptev I am using Lazy Loader on desktop. It works perfectly. But on a smaller screen, it isn't. All images are getting loaded even if not in the viewport. What is going wrong here ?

Link

Need support for background images

First of all, Great package. Currently this module supports only image tags. Can we please make it work for background-images too ? That would be really helpful.

Error in your basic example.

I can't run your basic example. When I try to start the server, suddenly throw the Cannot find module babel-core/register error displayed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.