Skip to content

Instantly share code, notes, and snippets.

@RusseII
Created May 19, 2020 21:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RusseII/62a304c29af402292ef4c57a1cf1a988 to your computer and use it in GitHub Desktop.
Save RusseII/62a304c29af402292ef4c57a1cf1a988 to your computer and use it in GitHub Desktop.
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