Skip to content

<1Kb React hook for resolving promises with Suspense support.

License

Notifications You must be signed in to change notification settings

zakdances/react-promise-suspense

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 => console.log(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>
  );
};

About

<1Kb React hook for resolving promises with Suspense support.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 50.4%
  • JavaScript 23.2%
  • HTML 13.5%
  • CSS 12.9%