过监听 iframe 的 onLoad 事件来确定其加载完成
函数组件使用方法,
import React, { useEffect, useRef, useState } from 'react';
const iframeRef = useRef(null);
const handleLoad = () => {
console.log('加载完成')
setShowload(false)
};
useEffect(() => {
const iframe = iframeRef.current;
iframe.addEventListener('load', handleLoad);
return () => {
if (iframeRef.current) {
iframeRef.current.src = '';
}
iframe.removeEventListener('load', handleLoad);
};
}, [])
<iframe
ref={iframeRef}
src={`http://192.201.201.158:3009 `}
allowFullScreen={true}
scrolling={'no'}
height="100%" width="100%"></iframe>
使用了 useRef
和 useEffect
来监听 iframe 的 onLoad
事件,在 handleLoad
中执行我们想要的操作。