0.2.1 • Published 4 years ago

@codefeathers/use-promise v0.2.1

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

React use-promise

Use React Hooks to resolve a promise; a wrapper around useState and useEffect for data-fetching.

Installation

npm i @codefeathers/use-promise

Usage

import { usePromise } from "@codefeathers/use-promise";

const Search = () => {

	const [ search, setSearch ] = useState("");

	// if the Promise rejects, error is set, otherwise response is set
	const [ response, error, loading ] = usePromise(
		// Function that returns a Promise
		() => fetch(`/api/search?s=${search}`).then(x => x.json()),
		// default state of response
		[],
		// List of dependencies to watch for
		[ search ],
	);

	return <>
		<input value={search} onChange={e => setSearch(e.target.value)} />
		{loading ? <p>Loading...</p> : ""}
		{error ? <p>There was an error</p> :
			<ol>
				{response.map(each =>
					<li key={each.id}>{each.content}</li>)}
			</ol>
		}
	<>;

};