# use-fetch-image

> React hook to fetch an image

Latest version **1.0.3** (published 2021-12-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-fetch-image
pnpm add use-fetch-image
yarn add use-fetch-image
bun add use-fetch-image
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2021-12-15 |
| First published | 2021-12-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Bun Wong |
| Maintainers | hdwong |
| Keywords | react, react-hooks, image, use-image, fetch |

## Links

- npm: https://www.npmjs.com/package/use-fetch-image
- Repository: https://github.com/hdwong/use-fetch-image
- Homepage: https://github.com/hdwong/use-fetch-image#readme
- Issues: https://github.com/hdwong/use-fetch-image/issues
- npm.io page: https://npm.io/package/use-fetch-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

- 1.0.3 (latest) — 2021-12-15
- 1.0.2 — 2021-12-15
- 1.0.1 — 2021-12-15
- 1.0.0 — 2021-12-15

## README

# use-fetch-image
React hook for fetch an image with the download percentage.

[Demo](https://codepen.io/hdwong/pen/jOGyoZx)

## Install
Yarn
```
yarn add use-fetch-image
```

NPM
```
npm install use-fetch-image
```

## Usage
```js
import { useImage } from "use-fetch-image";

export default () => {
  const url = 'https://picsum.photos/400/300';  // demo picture
  const [ image, status, progress ] = useImage(url);

  return (!!image && status === 'loaded') ? (
    <img src={image.src} alt="use-image" />
  ) : (
    <div>Loading... ({(progress * 100) << 0}%)</div>
  );
}
```

## API
| Returns    | Desc |
|------------|------|
| `image`    | the image DOM element or `undefined` before image is loaded |
| `status`   | the download status, including `"loading"`, `"loaded"`, `"error"` |
| `progress` | the download percentage, it is a floating point number, from `0` to `1` |

> **Note**\
`progress` calculation depends on the `content-length` value in the HTTP header.\
Some image servers will not set `content-length` in the HTTP header when responding.\
At this time, the progress will be set to `1` when the download status is `"loaded"`.

> **Note**\
This hook is fully compatible with [use-image](https://www.npmjs.com/package/use-image) in `konvajs`.

## License
MIT

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