Created
July 23, 2021 15:59
-
-
Save kauly/c16e5fd88240ac6990d22fa4cabbe276 to your computer and use it in GitHub Desktop.
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
import * as React from 'react' | |
const useMediaDownload = (src: string) => { | |
const [loading, setLoading] = React.useState(false) | |
const [url, setUrl] = React.useState<string>() | |
React.useEffect(() => { | |
if (!src) return | |
;(async () => { | |
setLoading(true) | |
const res = await fetch(src) | |
const blob = await res.blob() | |
setUrl(URL.createObjectURL(blob)) | |
setLoading(false) | |
})() | |
return () => { | |
url && URL.revokeObjectURL(url) | |
} | |
}, [src]) | |
return { loading, url } | |
} | |
export default useMediaDownload |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
just a scratch, there is space for improvement