
Security News
Deno 2.4 Brings Back deno bundle, Improves Dependency Management and Observability
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.
react-use-promise
Advanced tools
React hook for handling promises.
While this works and is an interesting use of hooks, it might be a better idea to use Suspense when dealing with promises.
Suspense isn't still fully released, but you can start using it with
React.lazy
.
Using npm:
$ npm install --save react-use-promise
Using yarn:
$ yarn add react-use-promise
Since this module uses React's new Hooks feature,
to try this out you'll need to install at least version 16.8.0
of react
and react-dom
:
$ yarn add react@^16.8.0 react-dom@^16.8.0
import React, { useMemo } from 'react';
import usePromise from 'react-use-promise';
function Example() {
const [result, error, state] = usePromise(
() => new Promise(resolve => {
setTimeout(() => resolve('foo'), 2000);
}),
[]
);
return (
<div>
<p>{state}</p>
<p>{result || error}</p>
</div>
);
}
usePromise<Result, Error>(
Promise<Result, Error> | () => Promise<Result, Error>,
Array<any>
): [
Result,
Error,
'pending' | 'resolved' | 'rejected'
]
Receives a promise or a function that returns a promise and returns an array
with the promise's result, error and state. The state is a string that can
have one of three values: 'pending'
, 'resolved'
or 'rejected'
.
Note: You'll need to pass the inputs array to usePromise
, otherwise
this will try to resolve the promise on every render. For example:
const [response, error] = usePromise(
() => fetch(url),
[url]
);
This will only call fetch
again when the url
changes.
If you only want to resolve the promise once, pass an empty array, like this:
const [result, error] = usePromise(
() => Notification.requestPermission(),
[]
);
Clone the repo and install the dependencies by running yarn
on the project's
root directory (or npm install
, if you don't have yarn installed).
Tests can be run with yarn test
(or npm test
) and there's an example
application that can be run with yarn example
(or npm run example
).
Please feel free to submit any issues or pull requests.
MIT
FAQs
React hook for handling promises.
The npm package react-use-promise receives a total of 15,235 weekly downloads. As such, react-use-promise popularity was classified as popular.
We found that react-use-promise demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.
Security News
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.