Skip to content

Commit 3529e5c

Browse files
committed
Refactor useScript
1 parent 78360d3 commit 3529e5c

File tree

1 file changed

+2
-18
lines changed

1 file changed

+2
-18
lines changed

index.js

+2-18
Original file line numberDiff line numberDiff line change
@@ -1075,9 +1075,7 @@ export function useScript(src, options = {}) {
10751075
const cachedScriptStatuses = React.useRef({});
10761076

10771077
React.useEffect(() => {
1078-
if (!src) {
1079-
return;
1080-
}
1078+
if (!src) return;
10811079

10821080
const cachedScriptStatus = cachedScriptStatuses.current[src];
10831081
if (cachedScriptStatus === "ready" || cachedScriptStatus === "error") {
@@ -1088,26 +1086,12 @@ export function useScript(src, options = {}) {
10881086
let script = document.querySelector(`script[src="${src}"]`);
10891087

10901088
if (script) {
1091-
setStatus(
1092-
script.getAttribute("data-status") ?? cachedScriptStatus ?? "loading"
1093-
);
1089+
setStatus(cachedScriptStatus ?? "loading");
10941090
} else {
10951091
script = document.createElement("script");
10961092
script.src = src;
10971093
script.async = true;
1098-
script.setAttribute("data-status", "loading");
10991094
document.body.appendChild(script);
1100-
1101-
const setAttributeFromEvent = (event) => {
1102-
const scriptStatus = event.type === "load" ? "ready" : "error";
1103-
1104-
if (script) {
1105-
script.setAttribute("data-status", scriptStatus);
1106-
}
1107-
};
1108-
1109-
script.addEventListener("load", setAttributeFromEvent);
1110-
script.addEventListener("error", setAttributeFromEvent);
11111095
}
11121096

11131097
const setStateFromEvent = (event) => {

0 commit comments

Comments
 (0)