# react-render-image

> Render an image in React.

Latest version **3.0.1** (published 2020-09-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-render-image
pnpm add react-render-image
yarn add react-render-image
bun add react-render-image
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2020-09-14 |
| First published | 2017-12-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 14.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 7 |
| Maintainers | jameslnewell |
| Keywords | react, react-load-image, react-image-load, image, load, img, render, render-prop, hook, use-image, use-image |

## Links

- npm: https://www.npmjs.com/package/react-render-image
- Repository: https://github.com/jameslnewell/react-render-image
- Homepage: https://github.com/jameslnewell/react-render-image#readme
- Issues: https://github.com/jameslnewell/react-render-image/issues
- npm.io page: https://npm.io/package/react-render-image

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 3.0.1 (latest) — 2020-09-14
- 2.0.0-preview.1 (next) — 2020-02-14
- 3.0.0 — 2020-09-14
- 2.1.0 — 2020-07-08
- 2.0.0 — 2020-05-28
- 1.1.3 — 2018-12-18
- 1.1.2 — 2018-02-25
- 1.1.1 — 2017-12-14
- 1.1.0 — 2017-12-14
- 1.0.2 — 2017-12-11
- 1.0.1 — 2017-12-11
- 1.0.0 — 2017-12-11

## README

# react-render-image

[![Build Status](https://travis-ci.org/jameslnewell/react-render-image.svg?branch=master)](https://travis-ci.org/jameslnewell/react-render-image)

Render an image in React.

## Installation

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

## Usage

[Example](https://jameslnewell.github.io/react-render-image/) ([source](https://github.com/jameslnewell/react-render-image/blob/master/example/App.js))

```js
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

### &lt;Image/&gt;

#### Props

##### src (required)

> `string`

The image URI.

##### srcset

> `string`

The image URIs to use given various conditions. See the [MDN](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/srcset) for further detail.

##### sizes

> `string`

The width of the image given various conditions. See the [MDN](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/sizes) 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](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/srcset) for further detail.
* `sizes` - The image URIs to use given various conditions. See the [MDN](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/sizes) 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()`.

---
_Source: https://npm.io/package/react-render-image · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
