Created
April 9, 2023 12:10
-
-
Save tjinlag/3e4fe2236621119f807ff91c2577768a to your computer and use it in GitHub Desktop.
React Custom Hook: useScript
This file contains 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 useAsync from "./useAsync" | |
export default function useScript(url) { | |
return useAsync(() => { | |
const script = document.createElement("script") | |
script.src = url | |
script.async = true | |
return new Promise((resolve, reject) => { | |
script.addEventListener("load", resolve) | |
script.addEventListener("error", reject) | |
document.body.appendChild(script) | |
}) | |
}, [url]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment