react-promise-suspense

React hook for resolving promises with Suspense support

MIT 7 个版本
安装
npm install react-promise-suspense
yarn add react-promise-suspense
pnpm add react-promise-suspense
bun add react-promise-suspense
README

usePromise

React hook for resolving promises with Suspense support.

Inspired by fetch-suspense, but this one is not limited to fetch, usePromise works with any Promise.

version minified size minzipped size downloads

Install

  • npm install react-promise-suspense --save

Example

import usePromise from 'react-promise-suspense';

const fetchJson = input => fetch(input).then(res => res.json());

const MyFetchingComponent = () => {
  // usePromise(Promise, [inputs,],)
  const data = usePromise(fetchJson, [
    'https://pokeapi.co/api/v2/pokemon/ditto/',
    { method: 'GET' },
  ]);

  return <pre>{JSON.stringify(data, null, 2)}</pre>;
};

const App = () => {
  return (
    <Suspense fallback="Loading...">
      <MyFetchingComponent />
    </Suspense>
  );
};
版本列表
0.3.4 2023-02-12
0.3.3 2020-05-03
0.3.2 2019-12-09
0.3.1 2019-08-01
0.3.0 2019-03-11
0.2.1 2019-03-11
0.2.0 2019-03-11