-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
executable file
·61 lines (52 loc) · 1.96 KB
/
index.js
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
var React = require('react');
module.exports = function useImage(url, crossOrigin, referrerpolicy) {
// lets use refs for image and status
// so we can update them during render
// to have instant update in status/image when new data comes in
const statusRef = React.useRef('loading');
const imageRef = React.useRef();
// we are not going to use token
// but we need to just to trigger state update
const [_, setStateToken] = React.useState(0);
// keep track of old props to trigger changes
const oldUrl = React.useRef();
const oldCrossOrigin = React.useRef();
const oldReferrerPolicy = React.useRef();
if (oldUrl.current !== url || oldCrossOrigin.current !== crossOrigin || oldReferrerPolicy.current !== referrerpolicy) {
statusRef.current = 'loading';
imageRef.current = undefined;
oldUrl.current = url;
oldCrossOrigin.current = crossOrigin;
oldReferrerPolicy.current = referrerpolicy;
}
React.useLayoutEffect(
function () {
if (!url) return;
var img = document.createElement('img');
function onload() {
statusRef.current = 'loaded';
imageRef.current = img;
setStateToken(Math.random());
}
function onerror() {
statusRef.current = 'failed';
imageRef.current = undefined;
setStateToken(Math.random());
}
img.addEventListener('load', onload);
img.addEventListener('error', onerror);
crossOrigin && (img.crossOrigin = crossOrigin);
referrerpolicy && (img.referrerPolicy = referrerpolicy);
img.src = url;
return function cleanup() {
img.removeEventListener('load', onload);
img.removeEventListener('error', onerror);
};
},
[url, crossOrigin, referrerpolicy]
);
// return array because it is better to use in case of several useImage hooks
// const [background, backgroundStatus] = useImage(url1);
// const [patter] = useImage(url2);
return [imageRef.current, statusRef.current];
};