Created
January 28, 2021 01:02
-
-
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)
This file contains hidden or 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
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