@desync/use-promise
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

usePromise

Stable Release codecov npm gzip size NPM

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

Package Sidebar

Install

npm i @desync/use-promise

Weekly Downloads

305

Version

1.2.0

License

MIT

Unpacked Size

36.8 kB

Total Files

16

Last publish

Collaborators

  • belgattitude