react-use-promise-hook
v0.2.0
Published
A hook for easily handling async promise-based tasks
Downloads
1
Maintainers
Readme
usePromise
To install run
npm i react-use-promise-hook
or
yarn add react-use-promise-hook
Usage
const counterPromise = () => {
return new Promise(res => {
setTimeout(() => res(Math.random()), 1000);
});
};
const Example = () => {
const [counter, errorMsg, loading, reset] = usePromise(counterPromise);
if (loading) {
return 'Loading...';
} else if (errorMsg){
return errorMsg;
} else {
return (
<>
<p>{counter}</p>
<button onClick={reset}>Reset counter</button>
</>
);
}
};