Coder Social home page Coder Social logo

react-imageloader's Introduction

react-imageloader

๐Ÿšจ This project is not maintained! ๐Ÿšจ

We are no longer using this component in our day-to-day work, so unfortunately, we have neglected maintenance of it for quite some time. Among the reasons why we haven't been using this component are:

However, it may still work for you. If you are looking for something like this, but don't want to take on an unmaintained dependency, check out this fork.

See the support matrix below if you are determined to use this.


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.createFactory('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.createFactory('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>
    );
  }
})

Design Decisions (mistakes?)

Since v2.0, 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. While this shouldn't be a problem for the vast majority of use cases, it can cause weirdness when browser caching is disabled (i.e., images loading twice, preloaders disappearing before the image is ready).

Supported versions of React

React ImageLoader
<0.13 1.x

=0.13, <15 | 2.x =15 | 3.x

react-imageloader's People

Contributors

dbachrach avatar konclave avatar lettertwo avatar markmeeus avatar matthewwithanm avatar nivekmai avatar qwtel avatar simek avatar spike886 avatar ta2edchimp 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  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

react-imageloader's Issues

Preload w/ Failed Message Display together on Fail

Right now if the Image fails to load, the preloader stays and shows the Error Message.

Adding a simple check that it's loaded and not failed would do the trick...

if (this.props.preloader && this.state.status !== Status.LOADED && this.state.status !== Status.FAILED) {
      wrapperArgs.push(this.props.preloader());
}

Not sure on the desired functionality.

A question: how to use to wait until image is available

Hi, my use case is to wait (show spinner) until the image is available on the server (i.e. show spinner even in case of 404 and try to load). Does this component fit for such a case? Can you suggest how it's better to make it wait?

Image onLoad callbacks never fire

We're hitting a very strange bug where the first time a page loads in our app (i.e. after an install) the images we try to load with react-imageloader fail to fire the onLoad callback.

Here's a perma-link to the way we're using the component right now: https://github.com/mozilla/webmaker-android/blob/ef9a69050406c823c238ffbf7590705d717f00a0/www_src/components/card/card.jsx

I haven't been able to figure out exactly why the image (apparently created using React.DOM.img) never fires its' onLoad event.

Not support any events

You can't pass smth like onClick handler, which means you need to wrap ImageLoader element. Which is too complex/ugly for sucjh simple task

Does not work with dynamic routes

When working with dynamic routes in react-router, I'm getting the following errors:

Uncaught Error: Invariant Violation: findComponentRoot(..., .0.2.0.0.1.1.1.0.0.1:1.0.1): Unable to find
element. This probably means the DOM was unexpectedly mutated (e.g., by the browser), usually due to 
forgetting a <tbody> when using tables, nesting tags like <form>, <p>, or <a>, or using non-SVG 
elements in an <svg> parent. Try inspecting the child nodes of the element with React ID ``.

Set className of <img>?

There is no way to set className of the rendered <img> tag. This is important to me, and used to work in earlier versions of this library...

The behavior is due to the fact that className is in the property "blacklist".

Perhaps an "imgClassName" prop could be added to address this?

behavior when src is null

I assume when src is null or "" or undefined the "image not found" area should be loaded? Or this is not what it is expected to do? Thanks.

onError the preloader should be used

The default behavior should display the preloaded image if the specified image fails to load. Currently nothing is rendered if an image fails to load. We have onError, which is great. Something like postLoader or errorLoader so we can display something when an error occurs.

This can be done easily enough:

React.createClass
  getInitialState: -> hasError: false

  render: ->
    preloader = -> <img src = "cat.gif"/>    
    if @state.hasError
      preloader()
    else
      <ImageLoader 
        src       = { @props.src }
        preloader = { preloader }
        onError   = { => @setState hasError: true }
      />

Thoughts?

`alt` for image

Hi,

have you thought about adding alt for the image?

I can make PR, but it seems to be super easy.

Idea: timeout

Thanks for creating this component. Finding it very helpful!

Wanted to throw an idea out there. Happy to do a PR if you think its a good one.

I'd like the ability to add a timeout threshold to the image requests.

I want to abort the request if it takes longer than XXXXms. This would be helpful for slow/mobile connections.

plugin appears to disable browser caching

Someone opened a PR using this plugin on a project I run and it appears that after this plugin is used, the default React and browser behavior of caching the already requested images and instantly displaying them is disabled and the images are re-requested every time I go to this route. Is there a way to preserve this caching so the placeholder shows just once and from then on the cache is used to display the images instantly like React normally does?

Preloader vs Children

Hi,

I am using this component for showing user profiles. I have a default user picture that should be shown when

  1. The user image is not yet loaded (with preloader)
  2. The user image could not be loaded (via children)

When I provide both, sometimes it happens that my default image is shown twice.
When the preloader is defined and an error occurs, does the picture than stay returned by the preloader function then stay?

How do we use the standalone version ?

'ImageLoader' variable comes to be undefined. I went through the standalone version and found out the name to be 'ReactImageLoader', but that doesn't seem to be defined either. Kindly help me out.

It throws an error if <img> as children

Throws an error it try to load node as children

<ImageLoader src={this.props.item.get('imageUrl')}>
  <img src="/images/no_image.png"/>
</ImageLoader>

throws the following error

Uncaught Error: Invariant Violation: findComponentRoot(..., .0.1.1.0.1:$newReleases:$newReleases.1:$11603.0.0.0.0.0.0): Unable to find element. This probably means the DOM was unexpectedly mutated (e.g., by the browser), usually due to forgetting a <tbody> when using tables, nesting tags like <form>, <p>, or <a>, or using non-SVG elements in an <svg> parent. Try inspecting the child nodes of the element with React ID ``.

Resize image

I need to resize an image. I tried applying styles to <ImageLoader /> but the height and width ends up being attached to the <span> tag which is the parent of <img> tag.

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.