@supunlakmal/hooks
Version:
A collection of reusable React hooks
21 lines (20 loc) • 1.03 kB
TypeScript
type ScriptStatus = 'idle' | 'loading' | 'ready' | 'error';
interface UseScriptOptions {
/** HTML attributes to set on the script element (e.g., async, defer, integrity, crossOrigin) */
attrs?: Record<string, string | boolean>;
/** Optional callback to run when the script successfully loads */
onLoad?: () => void;
/** Optional callback to run if the script fails to load */
onError?: (error: Event | string) => void;
/** If true, removes the script tag when the component unmounts. Defaults to true. */
removeOnUnmount?: boolean;
}
/**
* Hook to dynamically load an external script and track its loading state.
*
* @param src The source URL of the script to load. Pass null/undefined to unload or skip loading.
* @param options Configuration options for loading and handling the script.
* @returns The loading status of the script ('idle', 'loading', 'ready', 'error').
*/
export declare function useScript(src: string | null | undefined, options?: UseScriptOptions): ScriptStatus;
export {};