UNPKG

use-async-effekt-hooks

Version:

React hooks for async effects and memoization with proper dependency tracking and linting support

72 lines 2.44 kB
import { DependencyList } from "react"; /** * A hook for handling async effects with proper dependency tracking and cleanup management. * The name is intentionally spelled with "k" to work correctly with react-hooks/exhaustive-deps ESLint rule. * * @param effect - An async function to execute. Receives an object with: * - `isMounted`: Function to check if the component is still mounted * - `waitForPrevious`: Function that returns a Promise to wait for the previous effect and its cleanup to complete * * The effect function can optionally return a cleanup function that can be either synchronous or asynchronous. * * @param deps - Dependency array for the effect (same as useEffect) * * @example * // Basic usage without waiting for previous effects * useAsyncEffekt(async ({ isMounted }) => { * const data = await fetchData(); * if (isMounted()) { * setData(data); * } * }, []); * * @example * // Usage with waiting for previous effect to complete * useAsyncEffekt(async ({ isMounted, waitForPrevious }) => { * await waitForPrevious(); // Wait for previous effect and cleanup * const data = await fetchData(); * if (isMounted()) { * setData(data); * } * }, [dependency]); * * @example * // Usage with synchronous cleanup * useAsyncEffekt(async ({ isMounted }) => { * const subscription = await createSubscription(); * * return () => { * subscription.unsubscribe(); // Sync cleanup * }; * }, []); * * @example * // Usage with asynchronous cleanup * useAsyncEffekt(async ({ isMounted }) => { * const connection = await establishConnection(); * * return async () => { * await connection.close(); // Async cleanup * }; * }, []); * * @example * // Complex usage with waiting and async cleanup * useAsyncEffekt(async ({ isMounted, waitForPrevious }) => { * await waitForPrevious(); // Ensure previous effect is fully cleaned up * * const resource = await acquireResource(); * if (isMounted()) { * setResource(resource); * } * * return async () => { * await resource.cleanup(); // Async cleanup * }; * }, [resourceId]); */ export declare function useAsyncEffekt(effect: ({ isMounted, waitForPrevious, }: { isMounted: () => boolean; waitForPrevious: () => Promise<void>; }) => Promise<void | (() => void | Promise<void>)>, deps?: DependencyList): void; //# sourceMappingURL=useAsyncEffekt.d.ts.map