Skip to content

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

NameTypeDefaultNote
srcstring-The source of the script
options?UseScriptOptions-The options of the script extends from attributes script tag
options.removeOnUnmount?booleantrueWhether to remove the script on unmount
options.async?booleantrueWhether 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 • Demo

Contributors ​

D
debabin
debabin
H
hywax
hywax

Released under the MIT License.