// 初始值设置为不是移动端 const [isMobile, setIsMobile] = useState<boolean>(false); const ua = navigator.userAgent.toLowerCase(); const agents = ['iphone', 'ipad', 'ipod', 'android', 'linux', 'windows phone']; // 所有可能是移动端设备的字段 useEffect(() => { for (let i = 0; i < agents.length; i++) { if (ua.indexOf(agents[i]) !== -1) { setIsMobile(true); } } // eslint-disable-next-line react-hooks/exhaustive-deps }, []);