import { useEffect , useState } from 'react' ; export default function useYoutubeAPI ( ) { const [ isYoutubeReady , setYoutubeReady ] = useState < boolean > ( false ) ; useEffect ( ( ) => { const tag = document . createElement ( 'script' ) ; tag . src = 'https://www.youtube.com/iframe_api' ; const firstScriptTag = document . getElementsByTagName ( 'script' ) [ 0 ] as any ; firstScriptTag . parentNode . insertBefore ( tag , firstScriptTag ) ; ( window as any ) . onYouTubeIframeAPIReady = ( ) => { if ( process . env . NODE_ENV === 'development' ) { console . log ( 'onYouTubeIframeAPIReady' ) ; } setYoutubeReady ( true ) ; ( window as any ) . onYouTubeIframeAPIReady = null ; delete ( window as any ) . onYouTubeIframeAPIReady ; } ; } , [ ] ) ; return [ isYoutubeReady ] ; }