Git Product home page Git Product logo

react-load-image's Introduction

react-load-image

This is a fork of https://github.com/hzdg/react-imageloader, however many design changes were made and deprecations fixed so it warranted its own repo/package.

This React component allows you to display content while waiting for the image to load, as well as by showing alternate content if the image fails to load.

Installing

npm install react-load-image

Usage

import React from 'react';
import ImageLoader from 'react-load-image';

function Preloader(props) {
  return <img src="spinner.gif" />;
}

React.render((
  <ImageLoader
    src="/path/to/image.jpg"
  >
    <img />
    <div>Error!</div>
    <Preloader />
  </ImageLoader>
), document.body);

Props

Name Type Description
onError function An optional handler for the [error] event.
onLoad function An optional handler for the [load] event.
src string The URL of the image to be loaded, will be passed as the src prop to your first child provided. If you want to use it as a background image, make your first child a react component like Name = (props) => <div style={{backgroundImage: props.src}}/> and do

Children

The first child of ImageLoader will be rendered when the image is successfully loaded. The src prop will be passed to it.

The second child of ImageLoader will be rendered when the image load fails.

The third child of ImageLoader will be rendered when the image is in the process of loading

Avoiding duplication Example

import React from 'react';
import ImageLoader from 'react-load-image';
import ImageError from './ImageError';
import ImageLoading from './ImageLoading';

const Image = (props) =>
  <ImageLoader {...props}>
    {this.props.children[0]}
    <ImageError />
    <ImageLoader />
  </ImageLoader>

export default Image;

import Image from './Image';

...
<Image style={{width: '50px'}}>
  <img style={{border: none}} />
</Image>
...

Using it as a backgroundImage

import React from 'react';

const BackgroundImage = ({src, style = {}, ...props} = {}) =>
  <div style={Object.assign({backgroundImage: `url(${src})`}, style)} {...props} />

export default BackgroundImage;

<Image style={{width: '50px'}}>

react-load-image's People

Contributors

arthurp1 avatar davidfurlong avatar dbachrach avatar lettertwo avatar matthewwithanm avatar nivekmai avatar qwtel avatar simek avatar spike886 avatar ta2edchimp avatar

Watchers

 avatar

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.