useScript
The useScript hook is used to load a script in the body. More Info...
import { useScript } from '@archibald/client';
useScript(PARAMETERS);
Parameters
| Name | Type | Description |
|---|---|---|
| id | string | Mandatory parameter. The unique id of the script |
| strategy | ScriptStrategyValue | Optional parameter. The strategy can be of type beforeHydration, afterInteractive or worker. Defaults to afterInteractive |
| active | boolean | Optional parameter. useEffect dependency. Script will load when active state is truthy for afterInteractive strategy |
| src | string | Optional parameter. Source url for the script. |
| stylesheets | string[] | Optional parameter. Link type elements that will be appended in the head of the document |
| noScript | boolean | Optional parameter. noscript type element that will be added in the body of the document |
| attr | Partial<HTMLScriptElement> | Optional parameter. A set of any HTMLScriptElement attributes that can be added additionally |
| children | ReactNode | Optional parameter. |
| onLoad | (element: any) => void | Optional parameter. Event that will be triggered once script is fully loaded |
| onError | (error: any) => void | Optional parameter. Event that will be triggered in case of an error |
Script api functionalities
| Name | Description |
|---|---|
| loadScript | Adds a script to the body using the default script strategy afterInteractive. |
| removeScript | Removes from document a script with a certain id with the option to persist the script in cache (default false) |
| getScriptFromCache | Retrieves a script with a certain id from cache data |
| addScriptToCache | Adds to cache a script with same params used in useScript hook |
| removeScriptFromCache | Removes a script identified by an id from cache |
| clearAllScriptCacheData | Clears all script-cache data |