Skip to content

Instantly share code, notes, and snippets.

@kauly
Created July 23, 2021 15:59
Show Gist options
  • Save kauly/c16e5fd88240ac6990d22fa4cabbe276 to your computer and use it in GitHub Desktop.
Save kauly/c16e5fd88240ac6990d22fa4cabbe276 to your computer and use it in GitHub Desktop.
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
@kauly
Copy link
Author

kauly commented Jul 23, 2021

just a scratch, there is space for improvement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment