Skip to main content

useScript

The useScript hook is used to load a script in the body. More Info...

import { useScript } from '@archibald/client';
useScript(PARAMETERS);

Parameters

NameTypeDescription
idstringMandatory parameter. The unique id of the script
strategyScriptStrategyValueOptional parameter. The strategy can be of type beforeHydration, afterInteractive or worker. Defaults to afterInteractive
activebooleanOptional parameter. useEffect dependency. Script will load when active state is truthy for afterInteractive strategy
srcstringOptional parameter. Source url for the script.
stylesheetsstring[]Optional parameter. Link type elements that will be appended in the head of the document
noScriptbooleanOptional parameter. noscript type element that will be added in the body of the document
attrPartial<HTMLScriptElement>Optional parameter. A set of any HTMLScriptElement attributes that can be added additionally
childrenReactNodeOptional parameter.
onLoad(element: any) => voidOptional parameter. Event that will be triggered once script is fully loaded
onError(error: any) => voidOptional parameter. Event that will be triggered in case of an error

Script api functionalities

NameDescription
loadScriptAdds a script to the body using the default script strategy afterInteractive.
removeScriptRemoves from document a script with a certain id with the option to persist the script in cache (default false)
getScriptFromCacheRetrieves a script with a certain id from cache data
addScriptToCacheAdds to cache a script with same params used in useScript hook
removeScriptFromCacheRemoves a script identified by an id from cache
clearAllScriptCacheDataClears all script-cache data