From 28ef34b4c7c8e9580a85d83d3dab11fde3be2db3 Mon Sep 17 00:00:00 2001 From: streamich Date: Sun, 8 Dec 2019 00:54:58 +0100 Subject: [PATCH] =?UTF-8?q?docs:=20=E2=9C=8F=EF=B8=8F=20add=20useUnmounted?= =?UTF-8?q?Promise=20reference?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- docs/useUnmountPromise.md | 30 ++++++++++++++++++++++++++++++ src/index.ts | 1 + 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 docs/useUnmountPromise.md diff --git a/README.md b/README.md index 6f370fb0..127ccab0 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ - [`useMountedState`](./docs/useMountedState.md) — track if component is mounted. - [`usePromise`](./docs/usePromise.md) — resolves promise only while component is mounted. - [`useLogger`](./docs/useLogger.md) — logs in console as component goes through life-cycles. - - [`useMount`](./docs/useMount.md) — calls `mount` callbacks. + - [`useMount`](./docs/useMount.md) and [`useUnmountPromise`](./docs/useUnmountPromise.md) — tracks if component is mounted. - [`useUnmount`](./docs/useUnmount.md) — calls `unmount` callbacks. - [`useUpdateEffect`](./docs/useUpdateEffect.md) — run an `effect` only on updates. - [`useIsomorphicLayoutEffect`](./docs/useIsomorphicLayoutEffect.md) — `useLayoutEffect` that does not show warning when server-side rendering. diff --git a/docs/useUnmountPromise.md b/docs/useUnmountPromise.md new file mode 100644 index 00000000..5a129cb9 --- /dev/null +++ b/docs/useUnmountPromise.md @@ -0,0 +1,30 @@ +# `useUnmountPromise` + +A life-cycle hook that provides a higher order promise that does not resolve if component un-mounts. + + +## Usage + +```ts +import useUnmountPromise from 'react-use/lib/useUnmountPromise'; + +const Demo = () => { + const mounted = useUnmountPromise(); + useEffect(async () => { + await mounted(someFunction()); // Will not resolve if component un-mounts. + }); +}; +``` + + +## Reference + +```ts +const mounted = useUnmountPromise(); + +mounted(promise); +mounted(promise, onError); +``` + +- `onError` — if promise rejects after the component is unmounted, `onError` + callback is called with the error. diff --git a/src/index.ts b/src/index.ts index 2559d82d..61ef03e3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -85,6 +85,7 @@ export { default as useTitle } from './useTitle'; export { default as useToggle } from './useToggle'; export { default as useTween } from './useTween'; export { default as useUnmount } from './useUnmount'; +export { default as useUnmountPromise } from './useUnmountPromise'; export { default as useUpdate } from './useUpdate'; export { default as useUpdateEffect } from './useUpdateEffect'; export { default as useUpsert } from './useUpsert';