useScript
Custom hook that dynamically loads scripts and tracking their loading status.
Installation
npx sse-tool add use-script
npm install sse-hooks
Usage
import { useScript } from "./{hooks file}";
const scriptStatus = useScript("https://example.com/script.js", {
removeOnUnmount: true,
});
// Access the status of the script loading (e.g., 'loading', 'ready', 'error').
import { useScript } from "sse-hooks";
const scriptStatus = useScript("https://example.com/script.js", {
removeOnUnmount: true,
});
// Access the status of the script loading (e.g., 'loading', 'ready', 'error').
API
Parameters
| Name | Type | Description |
| :------ | :----------------- | :------------------------------------------------------------ | ------------------------------------------------------------------------------------------ |
| src | string | any | The source URL of the script to load. Set to null or omit to prevent loading (optional). |
| options | UseScriptOptions | Additional options for controlling script loading (optional). |
Return Value
Returns UseScriptStatus.
The status of the script loading, which can be one of 'idle', 'loading', 'ready', or 'error'.