-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathuse-script.tsx
More file actions
68 lines (55 loc) · 2.02 KB
/
use-script.tsx
File metadata and controls
68 lines (55 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { useState, useEffect } from 'react';
export interface ScriptProps {
src: HTMLScriptElement['src'] | null;
checkForExisting?: Boolean;
[key: string]: any;
}
type ErrorState = ErrorEvent | null;
export default function useScript({
src,
checkForExisting = false,
...attributes
}: ScriptProps): [boolean, ErrorState] {
const initialLoadingState = checkForExisting ? false : Boolean(src);
const [loading, setLoading] = useState(initialLoadingState);
const [error, setError] = useState<ErrorState>(null);
useEffect(() => {
if (!isBrowser || !src) return;
if (checkForExisting) {
const existing = document.querySelectorAll(`script[src="${src}"]`);
if (existing.length > 0) {
setLoading(false);
return;
} else {
setLoading(true);
}
}
const scriptEl = document.createElement('script');
scriptEl.setAttribute('src', src);
Object.keys(attributes).forEach((key) => {
if (scriptEl[key] === undefined) {
scriptEl.setAttribute(key, attributes[key]);
} else {
scriptEl[key] = attributes[key];
}
});
const handleLoad = () => {
setLoading(false);
};
const handleError = (error: ErrorEvent) => {
setError(error);
};
scriptEl.addEventListener('load', handleLoad);
scriptEl.addEventListener('error', handleError);
document.body.appendChild(scriptEl);
return () => {
scriptEl.removeEventListener('load', handleLoad);
scriptEl.removeEventListener('error', handleError);
};
// we need to ignore the attributes as they're a new object per call, so we'd never skip an effect call
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [src]);
return [loading, error];
}
const isBrowser =
typeof window !== 'undefined' && typeof window.document !== 'undefined';