UNPKG

624 BTypeScriptView Raw
1/**
2 * Track whether a component is current mounted. Generally less preferable than
3 * properlly canceling effects so they don't run after a component is unmounted,
4 * but helpful in cases where that isn't feasible, such as a `Promise` resolution.
5 *
6 * @returns a function that returns the current isMounted state of the component
7 *
8 * ```ts
9 * const [data, setData] = useState(null)
10 * const isMounted = useMounted()
11 *
12 * useEffect(() => {
13 * fetchdata().then((newData) => {
14 * if (isMounted()) {
15 * setData(newData);
16 * }
17 * })
18 * })
19 * ```
20 */
21export default function useMounted(): () => boolean;