2.0.1 • Published 3 years ago

use-promise-result v2.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

Tired creating these state in your component: loading, error, data, retry? Also you need to check if component unmounted, is the current response still valid ... With use-promise-result, your job is to specify how to get data, we take care all the boring flags.

Build Status:

npm publish

Install:

npm i use-promise-result

Usage:

import { usePromiseTracker } from "use-promise-result";

const dataProvider = async () => {
  return (await axios.get("https://jsonplaceholder.typicode.com/todos/1")).data;
};

function App() {
  const { data, error, loading, success, track, tracking } = usePromiseTracker(
    dataProvider()
  );

  const handleReload = () => track(dataProvider());

  // ...
}

Fetch data on click:

const { data, error, loading, success, track, tracking } = usePromiseTracker();

const handleReload = () => track(dataProvider());

API Reference:

usePromiseTracker

usePromiseTracker() : {data, error, loading, success, track, tracking}

Returned value

- data: value returned from the promise
- error: error throw from the promise
- loading: indicate state of the promise
- track: call this function to track another promise
- tracking: indicate that the hook is tracking a promise
- success: tracking && !state.loading && !state.error

Demo:

https://stackblitz.com/edit/react-kuzhwn?file=package.json

2.0.0-alpha.7

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

2.0.0-alpha.3

3 years ago

2.0.0-alpha.4

3 years ago

2.0.0-alpha.5

3 years ago

2.0.0-alpha.6

3 years ago

2.0.0-alpha.0

3 years ago

2.0.0-alpha.1

3 years ago

2.0.0-alpha.2

3 years ago

1.2.0

3 years ago

1.2.1

3 years ago

1.1.0

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago