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

0.1.0 • Public • Published

use-unmounted - react useUnmounted hook

do async operations in useEffect, and set state safety

Usage

import { useUnmounted } from '@-ft/use-unmounted';

function MyComponent() {
  const unmountedPersist = useUnmounted();
  const [state, setState] = useState("Loading...");
  useEffect(() => {
    (async () => {
      const result = await fetchSomething();
      if (unmountedPersist.current) {
        // if the component is unmounted, setState is not safe
        return;
      }
      setState(result);
    })();
  }, [unmountedPersist, setState]);
  return <div>{state}</div>;
}

Readme

Keywords

none

Package Sidebar

Install

npm i @-ft/use-unmounted

Weekly Downloads

2

Version

0.1.0

License

MIT

Unpacked Size

1.34 kB

Total Files

4

Last publish

Collaborators

  • mjy9088