Skip to content

Instantly share code, notes, and snippets.

@TSMMark
Created January 28, 2021 01:02
Show Gist options
  • Save TSMMark/aa473eb7b902bc32015c24ae4b51e743 to your computer and use it in GitHub Desktop.
Save TSMMark/aa473eb7b902bc32015c24ae4b51e743 to your computer and use it in GitHub Desktop.
React hook to return boolean true once the YouTube Iframe API is ready (onYouTubeIframeAPIReady)
const useYouTubeIframeAPIReady = (): boolean => {
const [isReady, setIsReady] = useState<boolean>(false)
useEffect(() => {
if (window.YT.Player) {
setIsReady(true)
return
}
window['onYouTubeIframeAPIReady'] = () => {
setIsReady(true)
}
return () => {
delete window['onYouTubeIframeAPIReady']
}
}, [setIsReady])
return isReady
}
export default useYouTubeIframeAPIReady
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment