Giter Site home page Giter Site logo

react-image-loadr's Introduction

react-image-loadr

A simple component for asynchronous image loading in React.

It uses the render prop pattern to provide you with the current loading state of your image.

Example

import React from 'react'
import ImageLoader from 'react-image-loadr'

import ErrorComponent from './error'
import LoaderComponent from './loader'

export default ({ src }) => (
  <ImageLoader src={src}>
    { ({ loading, image, error }) => {
      if (error) {
        return <ErrorComponent error={error} />
      }

      if (loading) {
        return <LoaderComponent />
      }

      return (
        <img src={image} alt='My asynchronously loaded image' />
      )
    } }
  </ImageLoader>
)

This example code renders the ErrorComponent if an error occurred (passing it the error which was thrown), the LoaderComponent while the image is beeing loaded and the image when it finished loading.

API

Component Properties

Property Description Example
src  The image source you'd normally pass to your img tag - most of the times the link to your image. '/assets/img.png'
children Your render function. See the following spec for details on the passed data. ({ image, loading }) => !loading && <img src={image} />

Rendering function arguments

react-image-loadr passes an object containing the current fetching state to your rendering function. It provides you with the follwing data:

Key | Description | Type --- | --- | --- image | The fetched image or null if the fetching failed/hasn't finished yet. | Image || null loading | Wether or not the image is currently loading. | Boolean error | If an error was thrown while fetching, it gets passed under this key. | Error || null

Feature Requests

If there is any feature you'd like to see for this component, reach out to me using the issues and mark it a [FEATURE REQUEST] using the issue title. I'll try to respond as fast as possible.

Contributing

Any contributions (code improvements, feature implementations) are welcome. Just open a PR with your changes and a small description or the reference to an issue. I'll look to review it - as fast as possible as well.

Thanks in advance and have fun using this component!

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.