useScript ​
Hook that manages a script with onLoad, onError, and removeOnUnmount functionalities
browser
test coverage
Last changed: last month
Installation ​
Library
CLI
Manual
typescript
import { useScript } from '@siberiacancode/reactuse';
Usage ​
typescript
const status = useScript('https://example.com/script.js');
Demo ​
Api ​
Parameters
Name | Type | Default | Note |
---|---|---|---|
src | string | - | The source of the script |
options? | UseScriptOptions | - | The options of the script extends from attributes script tag |
options.removeOnUnmount? | boolean | true | Whether to remove the script on unmount |
options.async? | boolean | true | Whether to load the script asynchronously |
Returns
UseScriptStatus
Type declaration ​
typescript
import type { ComponentProps } from 'react';
export type UseScriptStatus = 'error' | 'loading' | 'ready' | 'unknown';
export interface UseScriptOptions extends ComponentProps<'script'> {
/** Whether to remove the script on unmount */
removeOnUnmount?: boolean;
}
Source ​
Source • DemoContributors ​
D
H