https://github.com/doasync/use-promise
usePromise React hook
https://github.com/doasync/use-promise
Last synced: 8 months ago
JSON representation
usePromise React hook
- Host: GitHub
- URL: https://github.com/doasync/use-promise
- Owner: doasync
- License: mit
- Created: 2019-02-12T19:34:17.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T01:45:41.000Z (about 3 years ago)
- Last Synced: 2025-03-18T15:47:23.213Z (12 months ago)
- Language: JavaScript
- Size: 1.09 MB
- Stars: 9
- Watchers: 1
- Forks: 1
- Open Issues: 20
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# React `usePromise` hook
[![NPM Version][npm-image]][npm-url] ![NPM Downloads][downloads-image] [![GitHub issues][issues-image]][issues-url] [![Telegram][telegram-image]][telegram-url]
[npm-image]: https://img.shields.io/npm/v/use-promise.svg
[npm-url]: https://www.npmjs.com/package/use-promise
[downloads-image]: https://img.shields.io/npm/dw/use-promise.svg
[issues-image]: https://img.shields.io/github/issues/doasync/use-promise.svg
[issues-url]: https://github.com/doasync/use-promise/issues
[telegram-image]: http://i.imgur.com/WANXk3d.png
[telegram-url]: https://t.me/doasync
An optimized hook for handling promises in React
It supports `promise.cache` property (or function), which is used by some libraries and prevents unnecessary rerenders. You can use it too if you want
### Installation
```bash
npm install use-promise
```
or
```bash
yarn add use-promise
```
### Usage
Wrap a promise in `usePromise` hook and you will get back the following array: `[result, error, pending]`. Use array destructuring to get values that you need:
```js
const [company] = usePromise(fetchCompanyPromise)
const [users, usersError, usersLoading] = usePromise(fetchUsersPromise)
```
Do not call an async function inside `usePromise` to get a promise! You will need already prepared promise for this hook. If you don't have a promise, but you want to get it from an async call, then use `useAsyncCall` from [`use-call`](https://www.npmjs.com/package/use-call) package:
```js
const [user, userError, userLoading] = useAsyncCall(fetchUser, 120)
```
`useAsyncCall` hook uses `usePromise` from this package to get promise state
### Tip
If you found this hook useful, please star this package on [GitHub](https://github.com/doasync/use-promise) ★
### Author
@doasync