Giter Site home page Giter Site logo

react-image's Introduction

React Image ๐Ÿ ๐Ÿ– ๐Ÿœ

npm npm npm npm Known Vulnerabilities wercker status codecov

React Image is an <img> tag replacement for React.js, featuring preloader and multiple image fallback support.

With React Image you can specify multiple images to be used as fallbacks in the event that the browser couldn't load the previous image. Additionally, you can specify any React element to be used before an image is loaded (i.e. a spinner) and in the event than the specified image(s) could not be loaded.

React Image will cleverly hide "broken" images to prevent showing the browsers default "broken image" placeholder. React Image caches past attempts to load an image so that the same image won't be attempted to be pulled over the network again, until the next page reload.

This package was formerly known as react-img-multi. Special thanks to @yuanyan for agreeing to relinquish the name!

Getting started

  1. To include the code locally in ES6, CommonJS, or UMD format, install react-image using npm:
npm install react-image --save
  1. To include the code globally from a cdn:
<script src="https://unpkg.com/react-image/umd/index.min.js"></script>

Dependencies

react-image has no external dependencies, aside for the usual react and react-dom.

Documentation

Include react-image in your component:

// using an ES6 transpiler, like babel
import Img from 'react-image'

// otherwise
let Img = require('react-image')

and set a source for the image:

const myComponent = () => <Img src="www.example.com/foo.jpg">

will generate:

<img src="www.example.com/foo.jpg">

If the image cannot be loaded, <img> will not be rendered, preventing a "broken" image from showing.

Multiple fallback images:

When src is specified as an array, react-image will attempt to load all the images specified in the array, starting at the first and continuing until an image has been successfully loaded.

const myComponent = () =>
  <Img src={[
    'https://www.example.com/foo.jpg',
    'https://www.example.com/bar.jpg'
  ]}>

If an image has previously been attempted unsuccessfully, react-image will not retry loading it again until the page is reloaded.

Show a "spinner" or other element before the image is loaded:

const myComponent = () =>
  <Img
    src={[
      'https://www.example.com/foo.jpg',
      'https://www.example.com/bar.jpg'
    ]}
    loader={/*any valid react element */}
  >

If an image was previously loaded successfully (since the last time this page was loaded), the loader will not be shown and the image will be rendered directly instead.

Show a fallback element if non of the images could be loaded:

const myComponent = () =>
  <Img
    src={[
      'https://www.example.com/foo.jpg',
      'https://www.example.com/bar.jpg'
    ]}
    unloader={/*any valid react element */}
  >

Decoding before paint

By default and when supported by the browser, react-image uses img.decode() to decode the image and only render it when it's fully ready to be painted. While this doesn't matter much for vector images (such as svg's) which are rendered immediately, decoding the image before painting prevents the browser from hanging or flashing while the image is decoded. If this behaviour is undesirable, it can be disabled by setting the decode prop to false:

const myComponent = () =>
  <Img
    src={'https://www.example.com/foo.jpg'}
    decode={false}
  >

Recipes

Delay rendering until element is visible (lazy rendering)

By definition, React Image will try loading images as soon as the <Img> element is rendered in the DOM. This may be undesirable in some situations, such as when the page has many images. As with any react element, rendering can be delayed until the image is actually visible in the viewport using popular libraries such as react-visibility-sensor. Here is a quick sample (untested!):

import Img from 'react-image'
import VisibilitySensor from 'react-visibility-sensor'

const myComponent = () =>
  <VisibilitySensor>
    <Img src='https://www.example.com/foo.jpg'>
  </VisibilitySensor>

Browser Support

react-image does not include an Object.assign polyfill, that may be needed depending on your targeted browsers. You can add it in one of the following ways:

  1. include it in your package: https://www.npmjs.com/package/es6-object-assign

  2. Use Mozilla's polyfill: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill

  3. Include the following code before including react-image:

Object.assign||function(r){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var a in n)Object.prototype.hasOwnProperty.call(n,a)&&(r[a]=n[a])}return r};

License

react-image is available under the MIT License

react-image's People

Contributors

mbrevda avatar renovate-bot avatar greenkeeper[bot] avatar renovate[bot] avatar hum4n01d avatar lucretiel avatar

Watchers

James Cloos 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.