GithubHelp home page GithubHelp logo

react-image-loading's Introduction

React Image Loading

Load an image in React with loading placeholder and fallback, with maximum customizability.

Installation

npm i react-image-loading

Quick usage

This will render the out of the box implementation of the ImageLoading component, including a loading placeholder and fallback that have absolute positioning to fill the dimensions of the parent container. See below on how to implement custom rendering and behavior.

import { Img } from 'react-image-loading';

const MyComponent = props =>
    <div style={{minHeight: 150, position: 'relative'}}>
        <Img src={props.src} srcSet={props.srcSet} />
    </div>

Custom implementation

The out of the box Img provides a basic use case for loading images with a loading placeholder and fallback. To customize you can implement your own version of the Img component specific to your project needs. Below is an example that mimics the basic behavior of the provided Img component, but changes the color of the Fallback and LoadingPlaceholder:

import * as React from 'react';
import ImageLoading, { Fallback, LoadingPlaceholder } from 'react-image-loading';

const Img = (props) => (
    <ImageLoading>
        {(ref, status) => (
            <React.Fragment>
                {status === 'error' || !props.src
                    ? <Fallback style={{ backgroundColor: 'red'}} />
                    : <React.Fragment>
                        <img ref={ref} {...props} />
                        <LoadingPlaceholder
                            style={{ 
                                transition: 'opacity 0.5s', 
                                opacity: status === 'loading' ? 1 : 0, 
                                backgroundColor: 'blue' 
                            }}
                            animate={status === 'loading'}
                        />
                    </React.Fragment>
                }
            </React.Fragment>
        )}
    </ImageLoading>
);

export default Img;

You can also choose to create your own implementations of the Fallback and LoadingPlaceholder, or implement an entirely different logic altogether. Note: rendering the image tag with the provided ref is what triggers the loading of the image.

API

<ImageLoading>

Props

RenderPropsFn

Type: Function

Params

Returns

React.ReactNode

Ref

Type: Function

Params

  • imageElement HTMLImageElement | null

Returns

void

LoadState

The loading state of the image. Will always start at "loading", even before first render of the image, so it will immediately be put into loading state.

Type: 'loading' | 'complete' | 'error'

<LoadingPlaceholder>

Props

  • style React.CSSProperties?
  • animationStyle React.CSSProperties?
  • animationDuration number? Loop duration in ms
  • animate boolean?

<Fallback>

Props

  • style React.CSSProperties?

react-image-loading's People

Contributors

richardvanwill avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

jtalk

react-image-loading's Issues

getting a warning when component is unmounted before the image was loaded

getting a warning when component is unmounted before the image was loaded

react-dom.development.js:506 Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.

need to cancel the request on componentWillUnount

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.