# with-fetch

> HOC for fetching data

Latest version **2.0.0** (published 2018-04-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install with-fetch
pnpm add with-fetch
yarn add with-fetch
bun add with-fetch
```

## 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 | 2.0.0 |
| Published | 2018-04-21 |
| First published | 2018-01-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 70.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | shoulderkarlsson |
| Maintainers | shoulderkarlsson |

## Links

- npm: https://www.npmjs.com/package/with-fetch
- Repository: https://github.com/ShoulderKarlsson/with-fetch
- Homepage: https://github.com/ShoulderKarlsson/with-fetch#readme
- Issues: https://github.com/ShoulderKarlsson/with-fetch/issues
- npm.io page: https://npm.io/package/with-fetch

## Dependencies (3)

- [react](https://npm.io/package/react.md) ^16.2.0
- [react-dom](https://npm.io/package/react-dom.md) ^16.2.0
- [recompose](https://npm.io/package/recompose.md) ^0.26.0

## Recent versions

- 2.0.0 (latest) — 2018-04-21
- 1.0.0 — 2018-01-28

## README

# with-fetch

HOC for simple data fetching designed for [fetch api](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API).
If you pass it a request function that is not following the fetch api, the raw response will be returned to the final component in the `data` prop.

## Installation

`$ yarn add with-fetch`  
 `$ npm install with-fetch`

## How it works

The component exports two HOC's

* withFetch
* displayWhileLoading

### withFetch

This HOC is where the fetching is done. The HOC will fetch, parse and then pass the data to the final component.

#### Basic Usage

```js
import {withFetch} from 'with-fetch'
import {compose} from 'recompose'
import fetch from 'isomorphic-fetch'

export const MyComopnent = compose(
  withFetch(props => fetch(`http://myResource.com/${props.someResourceId}`))
)(({data, loading, error}) => {
  if (error) {
    return <MyErrorComponent error={error} />
  }

  if (loading) return <MySpinner />

  return <div>{data}</div>
})
```

### displayWhileLoading

This HOC displays a spinner, it comes with a somewhat ugly default spinner but accepts a custom spinner. The HOC is designed to take up 100% space of the wrapper component.

#### Basic Usage (Combined with withFetch)

```js
import {withFetch, displayWhileLoading} from 'with-fetch'
import {compose} from 'recompose'
import fetch from 'isomorphic-fetch'

const MyCustomSpinner = () => <div>Spinning into places</div>

const enhance = compose(
  withFetch(props => fetch(`http://myResource.com/${props.someResourceId}`)),

  // Your spinner will be displayed until the data is returned

  // ####################################################################
  // If you dont pass any argument, the HOC will render a default spinner
  // ####################################################################
  displayWhileLoading(MyCustomSpinner)
)

const MyComponent = enhance(({data, loading, error}) => {
  // If you dont want this inside your render, move this functionality into a HOC aswell!
  if (error) return <MyErrorComponent error={error} />
  return <div>{data}</div>
})
```

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