GithubHelp home page GithubHelp logo

fakhruddinabdi / react-imageloader Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hzdg/react-imageloader

0.0 2.0 0.0 600 KB

A React component for wrangling image loading

License: Other

Makefile 5.53% JavaScript 90.06% HTML 4.41%

react-imageloader's Introduction

react-imageloader

One of the hardest things to wrangle in the browser is loading. When images and other linked elements appear in the DOM, the browser makes decisions on when to load them that sometimes result in problems for a site and its users, such as FOUC, unexpected load ordering, and degraded performance when many loads are occurring.

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

Usage

import React from 'react';
import ImageLoader from 'react-imageloader';

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

React.render((
  <ImageLoader
    src="/path/to/image.jpg"
    wrapper={React.DOM.div}
    preloader={preloader}>
    Image load failed!
  </ImageLoader>
), document.body);

Props

Name Type Description
className string An optional class name for the wrapper component.
imgProps object An optional object containing props for the underlying img component.
onError function An optional handler for the error event.
onLoad function An optional handler for the load event.
preloader function An optional function that returns a React element to be shown while the image loads.
src string The URL of the image to be loaded.
style object An optional object containing styles for the wrapper component.
wrapper function A function that takes a props argument and returns a React element to be used as the wrapper component. Defaults to React.DOM.span.

Children

Children passed to ImageLoader will be rendered only if the image fails to load. Children are essentially alternate content to show when the image is missing or unavailable.

For example:

React.createClass({
  // This will only show if "notgonnaload.jpg" doesn't load.
  errorMessage() {
    return (
      <div>
        <h2>Something went wrong!</h2>
        <p>Not gonna load "notgonnaload.jpg". bummer.</p>
      </div>
    );
  },
  render() {
    return (
      <ImageLoader src="notgonnaload.jpg">
        {this.errorMessage()}
      </ImageLoader>
    );
  }
})

Upgrading to 2.x

If you are upgrading to the 2.x version, there are a couple of changes you should be aware of:

  • Since 2.0, ImageLoader requires React >= 0.13
  • Loading is done 'off DOM' in a JavaScript Image() (instead of hidden in the DOM via a React <img />), so values passed to the onLoad and onError callbacks will be the browser native values, not React's synthesized values. This should't be a problem for the vast majority of use cases, but it is technically an API change.

react-imageloader's People

Contributors

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

Watchers

 avatar  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.