useScriptTag
Dynamically load and unload an external script tag. The script is appended to document.head on mount and removed on unmount. Supports promise-based control, deduplication of identical scripts, and optional manual lifecycle management.
Dynamically load and unload an external script tag.
cdn.jsdelivr.net/npm/dayjs@1/dayjs.min.jsimport { const useScriptTag: (src: MaybeObservable<string>, onLoaded?: (el: HTMLScriptElement) => void, options?: DeepMaybeObservable<UseScriptTagOptions>) => UseScriptTagReturn
useScriptTag } from "@usels/web";
function function Component(): JSX.Element
Component() { const { const isLoaded$: ReadonlyObservable<boolean>
True once the script's load event has fired (or a pre-loaded element was found). Resets to false on unload().
isLoaded$ } = function useScriptTag(src: MaybeObservable<string>, onLoaded?: (el: HTMLScriptElement) => void, options?: DeepMaybeObservable<UseScriptTagOptions>): UseScriptTagReturn
Framework-agnostic dynamic <script> loader. Appends an external script tag
to document.head, deduplicates by src, and tracks the load lifecycle.
useScriptTag("https://cdn.example.com/lib.js", (el: HTMLScriptElement
el) => { any
console.any
log("Script loaded", el: HTMLScriptElement
el); });
return <JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div>{const isLoaded$: ReadonlyObservable<boolean>
True once the script's load event has fired (or a pre-loaded element was found). Resets to false on unload().
isLoaded$.ImmutableObservableBase<boolean>.get(trackingType?: TrackingType | GetOptions): {}
get() ? "Script loaded" : "Loading..."}</JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div>;}import { createScriptTag } from "@usels/web";
function Component() { "use scope" const { isLoaded$ } = createScriptTag("https://cdn.example.com/lib.js", (el) => { console.log("Script loaded", el); });
return <div>{isLoaded$.get() ? "Script loaded" : "Loading..."}</div>;}Manual control
Section titled “Manual control”Set manual: true to take full control of when the script loads and unloads.
import { useScriptTag } from "@usels/web";
function Component() { const { load, unload, isLoaded$ } = useScriptTag( "https://cdn.example.com/lib.js", undefined, { manual: true } );
return ( <div> <button onClick={() => load()}>Load</button> <button onClick={() => unload()}>Unload</button> <p>Status: {isLoaded$.get() ? "loaded" : "not loaded"}</p> </div> );}import { createScriptTag } from "@usels/web";
function Component() { "use scope" const { load, unload, isLoaded$ } = createScriptTag( "https://cdn.example.com/lib.js", undefined, { manual: true } );
return ( <div> <button onClick={() => load()}>Load</button> <button onClick={() => unload()}>Unload</button> <p>Status: {isLoaded$.get() ? "loaded" : "not loaded"}</p> </div> );}Wait for script load
Section titled “Wait for script load”load() accepts a waitForScriptLoad parameter (default true). When true, the returned promise resolves only after the script’s load event fires. Set to false to resolve immediately after the element is appended.
const { load } = useScriptTag("https://cdn.example.com/lib.js", undefined, { manual: true });
// Resolves after the `load` event firesawait load(true);
// Resolves immediately after appending to DOMawait load(false);With options
Section titled “With options”import { useScriptTag } from "@usels/web";
useScriptTag("https://cdn.example.com/lib.js", undefined, { async: true, // default: true defer: false, type: "text/javascript", // default: "text/javascript" crossOrigin: "anonymous", referrerPolicy: "no-referrer", nonce: "abc123", attrs: { "data-id": "my-lib" },});Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
src | MaybeObservable<string> | - Script src (MaybeObservable<string> — read at load time). |
onLoaded | (el: HTMLScriptElement) => void (optional) | - Optional callback invoked once the script’s load event fires. |
options | UseScriptTagOptions (optional) | - Mount-time-only options (immediate, manual, attrs, etc.). |
UseScriptTagOptions
Section titled “UseScriptTagOptions”Returns
Section titled “Returns”UseScriptTagReturn
| Name | Type | Description |
|---|---|---|
scriptTag$ | ReadonlyObservable<HTMLScriptElement | null> | - |
isLoaded$ | ReadonlyObservable<boolean> | True once the script’s load event has fired (or a pre-loaded element was found). Resets to false on unload(). |
load | (waitForScriptLoad?: boolean | undefined) => Promise<boolean | HTMLScriptElement> | - |
unload | () => void | - |