Skip to content

Instantly share code, notes, and snippets.

@ayal
Last active January 17, 2022 09:44
Show Gist options
  • Save ayal/588f60d0e088d483cf89a25f6e3ee559 to your computer and use it in GitHub Desktop.
Save ayal/588f60d0e088d483cf89a25f6e3ee559 to your computer and use it in GitHub Desktop.
image loaded hook
Object.defineProperty(Image.prototype, 'onload', {
set: function (fn) {
fn();
},
configurable: true,
});
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