Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React from 'react'
- const noop = () => {}
- function useScript({ onCreate = noop, onError = noop, onLoad = noop, url }) {
- const [isReady, setIsReady] = React.useState(false)
- React.useEffect(() => {
- const script = document.createElement('script')
- onCreate()
- script.src = url
- script.async = true
- script.onerror = () => {
- onError()
- }
- script.onload = () => {
- setIsReady(true)
- onLoad()
- }
- document.body.appendChild(script)
- return () => {
- document.body.removeChild(script)
- }
- }, [onCreate, onError, onLoad, url])
- return isReady
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement