use-promise

2.0.1 • Public • Published

React usePromise hook

NPM Version NPM Downloads GitHub issues Telegram

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

npm install use-promise

or

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:

  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 package:

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

Author

@doasync

Dependencies (1)

Dev Dependencies (32)

Package Sidebar

Install

npm i use-promise

Weekly Downloads

4

Version

2.0.1

License

MIT

Unpacked Size

9.26 kB

Total Files

9

Last publish

Collaborators

  • doasync