Coder Social home page Coder Social logo

jameslnewell / react-render-image Goto Github PK

View Code? Open in Web Editor NEW
8.0 3.0 4.0 4.38 MB

Render an image in React.

Home Page: https://jameslnewell.github.io/react-render-image/

JavaScript 0.50% TypeScript 99.14% HTML 0.36%
react render image load react-image-load react-load-image render-prop useimage use-image hook

react-render-image's Introduction

react-render-image

Build Status

Render an image in React.

Installation

npm install --save react-render-image
// OR
yarn add react-render-image

Usage

Example (source)

import React from 'react';
import {Image, useImage} from 'react-render-image';

const {image, loaded, errored} = useImage({src});

<Image src={src} loading="๐Ÿ”„" loaded="โœ…" errored="โŒ"/>

<Image src={src} loading="๐Ÿ”„" errored="โŒ">
  {({image, loaded, errored}) => {
    return <img src={image.src} width={image.width} height={image.height}/>;
  }}
</Image>

API

<Image/>

Props

src (required)

string

The image URI.

srcset

string

The image URIs to use given various conditions. See the MDN for further detail.

sizes

string

The width of the image given various conditions. See the MDN for further detail.

loading

React.Node

Rendered when the image is loading.

loaded

React.Node

Rendered when the image is loaded.

errored

React.Node

Rendered when the image is errored.

children

({ image?: Image; status: enum, loaded: boolean; errored: boolean; }) => React.Node

Called to render something when the image is loading, loaded or errored.

Parameters:

  • status - An enum indicating whether the image is loading, loaded or errored.
  • loaded - A boolean indicating whether the image has loaded.
  • errored - A boolean indicating whether the image has errored.
  • image - The Image object. This can be used to inspect the width and height of the image, or it can be drawn onto a canvas using ctx.drawImage().

Returns:

RectNode

onLoad

() => void

Called when the image has been loaded.

onError

() => void

Called when the image cannot be loaded.

useImage(options)

useImage({ src?: string; srcset: string[]; sizes: string[]; }) => {status, image?: Image}

Parameters:

  • src - An enum indicating whether the image is loading, loaded or errored.
  • srcset - The image URIs to use given various conditions. See the MDN for further detail.
  • sizes - The image URIs to use given various conditions. See the MDN for further detail.

Returns:

  • status - An enum indicating whether the image is loading, loaded or errored.
  • loaded - A boolean indicating whether the image has loaded.
  • errored - A boolean indicating whether the image has errored
  • image - The Image object. This can be used to inspect the width and height of the image, or it can be drawn onto a canvas using ctx.drawImage().

react-render-image's People

Contributors

dependabot[bot] avatar jameslnewell avatar zzarcon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

zzarcon

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.