react-use-promise-hook

0.2.0 • Public • Published

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>
			</>
		);
	}
};

Package Sidebar

Install

npm i react-use-promise-hook

Weekly Downloads

1

Version

0.2.0

License

none

Unpacked Size

4.26 kB

Total Files

8

Last publish

Collaborators

  • erodactyl