# react-processor-hook

> A React hook for step-based asynchronous data processing.

Latest version **1.1.0** (published 2019-10-30) · ISC license · 0 weekly downloads

## Install

```sh
npm install react-processor-hook
pnpm add react-processor-hook
yarn add react-processor-hook
bun add react-processor-hook
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2019-10-30 |
| First published | 2019-10-29 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 10.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | mcpar-land |
| Maintainers | mcpar-land |
| Keywords | react |

## Links

- npm: https://www.npmjs.com/package/react-processor-hook
- Repository: https://github.com/mcpar-land/react-processor-hook
- Homepage: https://github.com/mcpar-land/react-processor-hook#readme
- Issues: https://github.com/mcpar-land/react-processor-hook/issues
- npm.io page: https://npm.io/package/react-processor-hook

## Dependencies (1)

- [react](https://npm.io/package/react.md) ^16.11.0

## 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.1.0 (latest) — 2019-10-30
- 1.0.5 — 2019-10-30
- 1.0.3 — 2019-10-30
- 1.0.1 — 2019-10-29
- 1.0.0 — 2019-10-29

## README

# `react-processor-hook`

`useProcessor` is a React hook for processing data in steps, and exposing the currently calculating step as a state.

Once all steps are complete, the final result will be reflected in `output`.


```JSX
import React from 'react';
import { useProcessor } from 'react-processor-hook';

const TweetLoader = (props) => {

  const {
    output,
    complete,
    error,
    step,
    stepIndex
  } = useProcessor([
    ["Loading tweets...", async () => {
      //
      // ... fetch tweets from api
      //
      return tweets;
    }],
    ["Sorting by likes...", async tweets => {
      //
      // ... use some algorithm to sort tweets
      //
      return sortedTweets
    }],
    ["Saving to disc...", async sortedTweets => {
      //
      // ... save tweets to disc
      //
      return sortedTweets
    }]
  ]);

  if(!complete || error) {
    return (
      <div>
        <h2>Loading...</h2>
        <h3>{step}</h3>
        {error ? <p>{error}</p> : null}
      </div>
    );
  } else {
    return (
      <div>
        {
          output.forEach((tweet, index) => {
            return <Tweet data={tweet} key={index} />;
          })
        }
      </div>
    );
  }
}

export default TweetLoader;
```


The function takes an array of asynchronous methods, optionally with names.
```JSX

const {
  output,
  complete,
  error,
  step,
  stepIndex
} = useProcessor([
  async () => {
    //
    // ...
    //
    return dataOne;
  },
  ["Named step", async dataOne => {
    //
    // ...
    //
    return dataTwo;
  }],
  async dataTwo => {
    //
    // ...
    //
    return dataThree
  },
  ["Another named step", async dataThree => {
    //
    // ...
    //
    return dataFour
  }]
]);

```

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