Created
May 19, 2020 21:39
-
-
Save RusseII/62a304c29af402292ef4c57a1cf1a988 to your computer and use it in GitHub Desktop.
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 React, { useEffect, useRef, useState, useCallback } from 'react'; | |
type IframeGoogleDocsProps = { | |
url: string, | |
}; | |
export function IframeGoogleDoc({ url }: IframeGoogleDocsProps) { | |
const [iframeTimeoutId, setIframeTimeoutId] = useState<any>(); | |
const iframeRef: any = useRef(null); | |
const getIframeLink = useCallback(() => { | |
return `https://docs.google.com/gview?url=${url}&embedded=true`; | |
},[url]) | |
const updateIframeSrc = useCallback(() => { | |
if (iframeRef.current) { | |
iframeRef!.current!.src = getIframeLink(); | |
}}, [getIframeLink]) | |
useEffect(() => { | |
const intervalId = setInterval( | |
updateIframeSrc, 1000 * 3 | |
); | |
setIframeTimeoutId(intervalId) | |
}, [updateIframeSrc]) | |
function iframeLoaded() { | |
clearInterval(iframeTimeoutId); | |
} | |
return ( | |
<iframe | |
title="Candidate Document" | |
onLoad={iframeLoaded} | |
onError={updateIframeSrc} | |
ref={iframeRef} | |
style={{ width: "100%", height: "60vh" }} | |
src={getIframeLink()} | |
/> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment