Last active
January 17, 2022 09:44
-
-
Save ayal/588f60d0e088d483cf89a25f6e3ee559 to your computer and use it in GitHub Desktop.
image loaded hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.defineProperty(Image.prototype, 'onload', { | |
set: function (fn) { | |
fn(); | |
}, | |
configurable: true, | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const useImageLoaded = (src) => { | |
const [ready, setReady] = React.useState(false); | |
React.useEffect(() => { | |
if (src) { | |
const img = new Image(); | |
img.onload = () => { | |
setReady(true); | |
}; | |
img.src = src; | |
setTimeout(() => { | |
setReady(true) | |
}, 500); | |
} | |
}, [src]); | |
return ready; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment