@desync/use-promise
v1.2.0
Published
Typescript friendlier hook to deal with async.
Downloads
543
Readme
usePromise
Simple hook to call a promise
$ yarn add @desync/use-promise
Usage:
import ky from 'ky';
import { usePromise } from '@desync/use-promise';
type User = {
userId: number;
name: string;
username: string;
email: string;
};
const loadUsers = async (): Promise<User[]> => {
return ky
.get('https://jsonplaceholder.typicode.com/users', {
credentials: 'include',
})
.json<User[]>();
};
const UserList: React.FC = () => {
const { data, error, isLoading } = usePromise(loadUsers, {});
return (
<div>
{isLoading && <span>loading...</span>}
{error && <span>{error.message}</span>}
{data && <pre>{JSON.stringify(data)}</pre>}
</div>
);
};
Codesandbox
https://codesandbox.io/s/reverent-architecture-r5c11