0.0.3 • Published 3 years ago

@kodai3/use-promise v0.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

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);
    })();
  });
};