# @naveteam/dispatcher

> Dispatcher Component for React.js

Latest version **1.1.0** (published 2019-06-20) · ISC license · 0 weekly downloads

## Install

```sh
npm install @naveteam/dispatcher
pnpm add @naveteam/dispatcher
yarn add @naveteam/dispatcher
bun add @naveteam/dispatcher
```

## 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.1.0 |
| Published | 2019-06-20 |
| First published | 2018-08-13 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 249 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | João Pedro Bretanha |
| Maintainers | naveteam |

## Links

- npm: https://www.npmjs.com/package/@naveteam/dispatcher
- Repository: https://github.com/naveteam/dispatcher
- Homepage: https://github.com/naveteam/dispatcher#readme
- Issues: https://github.com/naveteam/dispatcher/issues
- npm.io page: https://npm.io/package/@naveteam/dispatcher

## Dependencies (4)

- [react](https://npm.io/package/react.md) ^16.8.6
- [core-js](https://npm.io/package/core-js.md) 3
- [react-dom](https://npm.io/package/react-dom.md) ^16.8.6
- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.4.5

## Recent versions

- 1.1.0 (latest) — 2019-06-20
- 1.0.0 — 2019-01-06
- 0.1.2 — 2018-08-22
- 0.1.1 — 2018-08-16
- 0.1.0 — 2018-08-16
- 0.0.9 — 2018-08-16
- 0.0.8 — 2018-08-14
- 0.0.7 — 2018-08-14
- 0.0.6 — 2018-08-14
- 0.0.5 — 2018-08-14
- 0.0.4 — 2018-08-14
- 0.0.3 — 2018-08-13
- 0.0.2 — 2018-08-13
- 0.0.1 — 2018-08-13

## README

# Dispatcher React Component

## Install

`React` version below `16.8`:

`yarn add @naveteam/dispatcher@1.0`

`React` version above `16.8`:

`yarn add @naveteam/dispatcher`

## Usage

### Without hooks

```jsx
import Dispatcher from '@naveteam/dispatcher'

const MyComponent = () => (
  <Dispatcher dispatch={() => fetch('https://jsonplaceholder.typicode.com/todos/1')}>
    {({isLoading, content, hasError, reload}) => (
    !isLoading
        ? <div>
            {JSON.stringify(content)}
        </div>
        : <span>loading...</span>
    )}
  </Dispatcher>
)
```

### With hooks

```jsx
import { useDispatcher } from '@naveteam/dispatcher'

const MyComponent = () => {
    const [isLoading, error, content, reload] = useDispatcher(() => fetch('https://jsonplaceholder.typicode.com/todos/1'))

    if (isLoading) {
        return (
            <span>loading...</span>
        )
    }

    if (error) {
        <span>{JSON.stringify(error)}</span>
    }

    return (
        <div>
            <p>{JSON.stringify(content)}</p>
            <button onClick={reload}>reload</button>
        </div>
    )
}
```

## Component Props

| Prop  |  value | Example | Required |
|---|---|---|---|
| dispatch | function that returns a promise |  `() => myPromise()` | yes |
| shouldUpdate  | Comparison of props to trigger shouldComponentUpdate  | ```<Dispatcher lastId={'0'} shouldUpdate={prevProps => prevProps.lastId !== lastId}>{...}</Dispatcher> ``` | no |
| errorCallback  | function triggered when an request error occurs  |  `error => console.log(error)` | no |

## Render Props

| Prop  | Explanation | Default Value |
|---|---|---|
| isLoading | Boolean indicating request has finished | true |
| content | value of finished promise | null |
| hasError | boolean indicating if request failed | false |
| reload | calls dispatch function

## Contribute

Run `yarn dev`

Edit `index.js` and `App.js` inside `src` folder

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