Code Monkey home page Code Monkey logo

react-lazy-image's Introduction

react-lazy-image

Component to render images and lazyload them if are in the viewport (or near to them).

This component extends from React.PureComponent so it needs React.js v15.3.0 or superior.

Installation

npm i -S react-lazy-image

Usage example

import Image from 'react-lazy-image';

const image = <Image source="https://placekitten.com/200/300" />;

API

onLayout({ element: Object }): void

Called everytime the component is rendered or updated. Default: () => {}.

onError({ element: Object, error: Error }): void

Called if the request to load the image failed. Default: () => {}.

onLoad({ element: Object }): void

Called after the load ended (either successfully or not). Default: () => {}.

onLoadEnd({ element: Object }): void

Called after the load ended successfully. Default: () => {}.

onLoadStart({ element: Object }): void

Called when the request started. Default: () => {}.

onAbort({ element: Object }): void

Called if the load of the image was aborted. Default: () => {}.

onProgress({ element: Object }): void

Called everytime the AJAX progress event is dispatched. Default: () => {}.

offset: ?number

Set the amount of pixel near the viewport the component should be to start the image load. Default: 0.

source: string

The image source path to load.

defaultSource: ?string

The default image source path or base64. If isn't defined then it uses a SVG animated spinner.

type: ?string

The format type of the image (png, svg+xml, jpg or gif). Default: *.

minLoaded: ?number

The minimum download percentaje to avoid aborting the request if the image leaves the viewport. Default: 50.

Common img attributes

This component allow the usage of common img attributes like alt, width, className, etc. So you can use it as a normal img tag, just change src to source and (if you want to) add the other optional props.

License

The MIT License (MIT)

Copyright (c) 2015 Sergio Daniel Xalambrí

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

react-lazy-image's People

Contributors

renovate-bot avatar renovate[bot] avatar sergiodxa 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

react-lazy-image's Issues

setState after unmount

setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the Image component.

Should probably check if the component is unmounted when doing asynchronous setState.

Listing react as a peerDependency instead of dependency

Hi there,
I've used your package in a project and it seems to fail the image loading, enter in the onError callback with this error:
this.updater.enqueueCallback is not a function
even though the image exists.
I've been looking around for the cases this error was thrown and this react issue thread explains that the package installation brings an older version than react since it's listed as a dependency not as a peerDependency:
facebook/react#10320 (comment)
Please, can you apply this quick fix ?

XHR Loading causes CORS issues

Hello,

Using this package causes CORS issues because of XHR requests for non-configured servers. This can be tricky when trying to load images from server where we don't have access to change CORS policy.

However with a 5 mins investigation : The Image() object can load images and bypass CORS validation. But it load it into a HTMLImageElement thus making it harder to inject with React (maybe with refs?). Plus I don't know if we can keep loading cancelation.

Internet Explorer Support

Related to #1 , in IE11 this error is shown in componentWillUnmount.

SCRIPT5007: Unable to get property 'abort' of undefined or null reference

Seems like this.request is null.

Microsoft Edge note

I recently received a task from my project manager, which she received from a project manager at Microsoft, making mention that in Microsoft Edge, images will not display if data/image is used with a wildcard (asterisk). This perhaps should be added as a warning to the readme?

Custom loading.

Can I send my custom loading component or ".gif" for loading indication?

lazyload in horizontal carousel

<SwipeableViews 
    className={`${prefixCls}__swiper`} 
    index={state.index}
    animateTransitions={state.animateTransitions}
    onChangeIndex={index => this.swiperChange(index)}
>
    {
        state.data.map((item, idx) => (
            <Image 
                className={`${prefixCls}__img`} 
                key={idx}
                source={item.url}
            />
        ))
    }
</SwipeableViews>

I have a Carousel but it's moving horizontally , so it just the first img has lazy and other do not work.

http://ww3.sinaimg.cn/mw690/715ed77dgw1f9g6x6iyj9g20b00cqkf1.gif

if there some trigger api ?

By the way my english is very bad, i hope you can help me, thank you!

Use IntersectionObserver instead of onScroll

Right now the component is using the window scroll element to check every time the user scroll if the image in in the viewport (or near to it). This can be a big problem if there are many images and doesn't let the component to receive what to listen if it's rendered in another scrollable element.

With IntersectionObserver is it possible to detect this in a better way and fallback to always render the image.

How to set defaultSource prop for custom loader?

I saw your comment on one issue: -

The defaultSource prop is used for that. Is documented on the readme.

But I'm not clear how to define defaultSource, can you please throw some light on it?

Thank you very much in advance.

Much appreciated!!

Separate the lazy loading logic from viewport logic

A common use case for lazyloading is also to do deferred loading of images - i.e, load a small thumbnail / blurry image first and in the background load the actual image up.

This logic is distinct from the viewport calculation one, and may or may not need it. Does it therefore make sense to split this into two components, something like

<LazyLoadWhenInViewport>
  <LazyImage />
</LazyLoadWhenInViewport>

So that one can directly use <LazyImage> without using the layout code if needed. Since the hooks for when to trigger the lazy loading are present in <LazyImage>, this could potentially allow more robust parent components to be used for a variety of use cases.

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.