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

0.0.3 • Public • Published

usePromise

React Lifecycle hook that returns a helper function for wrapping promises. Promises wrapped with this function will resolve only when component is mounted.

Usage

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

const Demo = ({ promise }) => {
  const mounted = usePromise();
  const [value, setValue] = useState();

  useEffect(() => {
    (async () => {
      const value = await mounted(promise);
      // This line will not execute if <Demo> component gets unmounted.
      setValue(value);
    })();
  });
};

Readme

Keywords

Package Sidebar

Install

npm i @kodai3/use-promise

Weekly Downloads

0

Version

0.0.3

License

MIT

Unpacked Size

7.36 kB

Total Files

8

Last publish

Collaborators

  • kodai