Skip to content

Commit d057a9b

Browse files
committed
Re-implement useScript
1 parent 0a4eb07 commit d057a9b

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ declare module "@uidotdev/usehooks" {
229229
options?: {
230230
removeOnUnmount?: boolean;
231231
}
232-
): "idle" | "loading" | "ready" | "error";
232+
): "unknown" | "loading" | "ready" | "error";
233233

234234
export function useSessionStorage<T>(
235235
key: string,

index.js

+34-13
Original file line numberDiff line numberDiff line change
@@ -1101,29 +1101,50 @@ export function useScript(src, options = {}) {
11011101
React.useEffect(() => {
11021102
let script = document.querySelector(`script[src="${src}"]`);
11031103

1104+
const domStatus = script?.getAttribute("data-status");
1105+
if (domStatus) {
1106+
setStatus(domStatus);
1107+
return;
1108+
}
1109+
11041110
if (script === null) {
11051111
script = document.createElement("script");
11061112
script.src = src;
11071113
script.async = true;
1114+
script.setAttribute("data-status", "loading");
11081115
document.body.appendChild(script);
1109-
}
11101116

1111-
const handleScriptLoad = () => setStatus("ready");
1112-
const handleScriptError = () => setStatus("error");
1117+
const handleScriptLoad = () => {
1118+
script.setAttribute("data-status", "ready");
1119+
setStatus("ready");
1120+
removeEventListeners();
1121+
};
11131122

1114-
script.addEventListener("load", handleScriptLoad);
1115-
script.addEventListener("error", handleScriptError);
1123+
const handleScriptError = () => {
1124+
script.setAttribute("data-status", "error");
1125+
setStatus("error");
1126+
removeEventListeners();
1127+
};
11161128

1117-
const removeOnUnmount = optionsRef.current.removeOnUnmount;
1129+
const removeEventListeners = () => {
1130+
script.removeEventListener("load", handleScriptLoad);
1131+
script.removeEventListener("error", handleScriptError);
1132+
};
11181133

1119-
return () => {
1120-
script.removeEventListener("load", handleScriptLoad);
1121-
script.removeEventListener("error", handleScriptError);
1134+
script.addEventListener("load", handleScriptLoad);
1135+
script.addEventListener("error", handleScriptError);
11221136

1123-
if (removeOnUnmount === true) {
1124-
script.remove();
1125-
}
1126-
};
1137+
const removeOnUnmount = optionsRef.current.removeOnUnmount;
1138+
1139+
return () => {
1140+
if (removeOnUnmount === true) {
1141+
script.remove();
1142+
removeEventListeners();
1143+
}
1144+
};
1145+
} else {
1146+
setStatus("unknown");
1147+
}
11271148
}, [src]);
11281149

11291150
return status;

usehooks.com/src/content/hooks/useScript.mdx

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ import StaticCodeContainer from "../../components/StaticCodeContainer.astro";
1717
scripts into a React component. It manages the loading and status of the
1818
script, allowing you to conditionally render components or perform actions
1919
based on whether the script has been successfully loaded or encountered an
20-
error. The hook keeps track of the script’s status, such as "loading,"
21-
"ready," or "error," and provides this status as a return value. Additionally,
20+
error. The hook keeps track of the script’s status and provides this status as a return value. Additionally,
2221
it offers options to remove the script when the component is unmounted,
2322
ensuring proper cleanup.
2423
</HookDescription>
@@ -38,7 +37,7 @@ import StaticCodeContainer from "../../components/StaticCodeContainer.astro";
3837
<div class="table-container">
3938
| Name | Type | Description |
4039
| ------ | ------ | ----------- |
41-
| status | string | This represents the status of the script load. Possible values are: `loading`, `ready`, and `error`. |
40+
| status | string | This represents the status of the script load, `loading`, `ready`, `error`, or `unknown`. An `unknown` script is one that previously exists in the document, but was not added via `useScript`. |
4241
</div>
4342
</div>
4443

0 commit comments

Comments
 (0)