Skip to content

Commit 0d2e15e

Browse files
committed
Update useScript implementation
1 parent 32dbf10 commit 0d2e15e

File tree

2 files changed

+14
-30
lines changed

2 files changed

+14
-30
lines changed

index.js

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,52 +1081,36 @@ export function useRenderInfo(name = "Unknown") {
10811081
}
10821082

10831083
export function useScript(src, options = {}) {
1084-
const [status, setStatus] = React.useState(() => {
1085-
if (!src) {
1086-
return "idle";
1087-
}
1088-
1089-
return "loading";
1090-
});
1091-
1092-
const cachedScriptStatuses = React.useRef({});
1084+
const [status, setStatus] = React.useState("loading");
1085+
const optionsRef = React.useRef(options);
10931086

10941087
React.useEffect(() => {
1095-
if (!src) return;
1096-
1097-
const cachedScriptStatus = cachedScriptStatuses.current[src];
1098-
if (cachedScriptStatus === "ready" || cachedScriptStatus === "error") {
1099-
setStatus(cachedScriptStatus);
1100-
return;
1101-
}
1102-
11031088
let script = document.querySelector(`script[src="${src}"]`);
11041089

1105-
if (!script) {
1090+
if (script === null) {
11061091
script = document.createElement("script");
11071092
script.src = src;
11081093
script.async = true;
11091094
document.body.appendChild(script);
11101095
}
11111096

1112-
const handleScriptStatus = (event) => {
1113-
const newStatus = event.type === "load" ? "ready" : "error";
1114-
setStatus(newStatus);
1115-
cachedScriptStatuses.current[src] = newStatus;
1116-
};
1097+
const handleScriptLoad = () => setStatus("ready");
1098+
const handleScriptError = () => setStatus("error");
1099+
1100+
script.addEventListener("load", handleScriptLoad);
1101+
script.addEventListener("error", handleScriptError);
11171102

1118-
script.addEventListener("load", handleScriptStatus);
1119-
script.addEventListener("error", handleScriptStatus);
1103+
const removeOnUnmount = optionsRef.current.removeOnUnmount;
11201104

11211105
return () => {
1122-
script.removeEventListener("load", handleScriptStatus);
1123-
script.removeEventListener("error", handleScriptStatus);
1106+
script.removeEventListener("load", handleScriptLoad);
1107+
script.removeEventListener("error", handleScriptError);
11241108

1125-
if (options.removeOnUnmount === true) {
1109+
if (removeOnUnmount === true) {
11261110
script.remove();
11271111
}
11281112
};
1129-
}, [src, options.removeOnUnmount]);
1113+
}, [src]);
11301114

11311115
return status;
11321116
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import StaticCodeContainer from "../../components/StaticCodeContainer.astro";
3838
<div class="table-container">
3939
| Name | Type | Description |
4040
| ------ | ------ | ----------- |
41-
| status | string | This represents the status of the script load. Possible values are: `idle`, `loading`, `ready`, and `error`. |
41+
| status | string | This represents the status of the script load. Possible values are: `loading`, `ready`, and `error`. |
4242
</div>
4343
</div>
4444

0 commit comments

Comments
 (0)