Socket
Socket
Sign inDemoInstall

react-loading-image

Package Overview
Dependencies
8
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-loading-image

A react image loader component


Version published
Weekly downloads
1.6K
increased by22.45%
Maintainers
1
Install size
14.6 kB
Created
Weekly downloads
 

Readme

Source

react-loading-image NPM version Dependency Status Greenkeeper badge

A react image loader component

preview

Installation

$ npm install --save react-loading-image

Usage

class Demo extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      value: null
    };
  }

  onChange = (e) => {
    this.setState({
      value: e.target.value
    });
  }

  render() {
    const {value} = this.state;
    return (
      <div>
        <h1>Copy a image URL here</h1>
        <input onChange={this.onChange}/>
        <p>
          value: {value}
        </p>
        <div>
          {value && (
            <ImageLoader
              src={value}
              loading={() => <div>Loading...</div>}
              error={() => <div>Error</div>}
              />
          )}
        </div>
      </div>
    );
  }
}

Advanced

You might want to customized such as <div/> as your final view, instead of <img/> tags, you can use image prop to replace any React element as you wish!

For example, using styled-components:

import styled from 'styled-components';

const PreviewImg = styled.div`
  background-image: url(${props => props.src});
  width: 100%;
  height: 300px;
  background-size: contain;
  background-repeat: no-repeat;
`;

class Demo extends React.Component {
  ...

  render() {
    const {value} = this.props;
    return (
      <div>
        <ImageLoader
          src={value}
          image={props => <PreviewImg {...props}/>} // change to your customized component
          loading={() => <div>Loading...</div>}
          error={() => <div>Error</div>}
          />
      </div>
    );
  }
}

Props

NameTypeDefaultDescription
srcstringnullImage URL
stylestyle ObjectnullPass style object to set image's style
classNamestringnullSet image's className
onLoad(img: Image) => voidnullThis function will be called when image is loaded
onError(err: Event) => voidnullThis function will be called when image is failed
loading() => React.Element<*>nullReturn a React element that will show when image is loading
error() => React.Element<*>nullReturn a React element that will show when image is crashed
image({src: string, width: number, height: number}) => React.Element<*>nullFinal result will render to this customized React element, if you don't assign this props default image will render into <img src={src} width={width} height={height}/>

Start example server

npm start

generate demo

npm run gh-pages

License

MIT © chilijung

FAQs

Last updated on 06 Sep 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc