# @hojihooks/use-async-dispatch

> React Hook to dispatch asynchronous action and shows the progress of the action.

Latest version **1.0.1** (published 2019-07-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install @hojihooks/use-async-dispatch
pnpm add @hojihooks/use-async-dispatch
yarn add @hojihooks/use-async-dispatch
bun add @hojihooks/use-async-dispatch
```

## 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.1 |
| Published | 2019-07-24 |
| First published | 2019-07-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Hoeon Woo |
| Maintainers | woohoeon |
| Keywords | react, hook, hooks, react hooks, react-redux, redux, dispatch, async, reducer, progress, action |

## Links

- npm: https://www.npmjs.com/package/@hojihooks/use-async-dispatch
- Repository: https://github.com/woohoeon/hojihooks
- Homepage: https://github.com/woohoeon/hojihooks#readme
- Issues: https://github.com/woohoeon/hojihooks/issues
- npm.io page: https://npm.io/package/@hojihooks/use-async-dispatch

## 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.1 (latest) — 2019-07-24
- 1.0.0 — 2019-07-24

## README

# @hojihooks/use-async-dispatch

React Hook to dispatch asynchronous action and shows the progress of the action.

## Installation

#### yarn

`yarn add @hojihooks/use-async-dispatch`

#### npm

`npm i @hojihooks/use-async-dispatch`

## Usage

```js
import React, { useEffect } from "react";
import ReactDOM from "react-dom";
import { createStore, applyMiddleware } from "redux";
import { Provider } from "react-redux";
import promiseMiddleware from "redux-promise";
import useAsyncDispatch from "@hojihooks/use-async-dispatch";

// Action Creators
async function fetchData(url) {
  try {
    const response = await fetch(url);
    const data = await response.json();
    return {
      type: "FETCH_DATA",
      payload: { data }
    };
  } catch (error) {
    return console.error(error);
  }
}

function showProgress() {
  return {
    type: "SHOW_PROGRESS"
  };
}

function hideProgress() {
  return {
    type: "HIDE_PROGRESS"
  };
}

// Reducers
function reducer(state = [], action) {
  switch (action.type) {
    case "FETCH_DATA":
      console.log(action.payload.data);
      return state;
    case "SHOW_PROGRESS":
      console.log("show progress");
      return state;
    case "HIDE_PROGRESS":
      console.log("hide progress");
      return state;
    default:
      return state;
  }
}

const store = createStore(reducer, {}, applyMiddleware(promiseMiddleware));

function App() {
  // Easy to use
  const fetchExample1 = useAsyncDispatch(fetchData);

  // Using options
  const callback = () => {
    console.log("success");
  };
  const opts = {
    showProgress,
    hideProgress
  };
  const fetchExample2 = useAsyncDispatch(fetchData, callback, opts);

  useEffect(() => {
    fetchExample1("https://api.github.com/orgs/nodejs");
    fetchExample2("https://api.github.com/gists");
  });
  return <h1>Hello Hojihooks</h1>;
}

const rootElement = document.getElementById("root");
ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  rootElement
);
```

### Arguments

| Argument | Type     | Description                                       | Required |
| -------- | -------- | ------------------------------------------------- | -------- |
| asyncAction  | function | Function to dispatch asynchronously | yes      |
| callback  | function | Callback function after asynchronous dispatch completion | no      |
| opts  | Object | showProgress: Function to dispatch to show progress, hideProgress: Function to dispatch to hide progress | no      |

### Return

| Return value | Type      | Description                                                     | Default value |
| ------------ | --------- | --------------------------------------------------------------- | ------------- |
| function          | function | Function wrapped around the async dispatch logic | null          |

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