判断项目运行在移动端还是pc端

// 初始值设置为不是移动端
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
}, []);
原文地址:https://www.cnblogs.com/chenbeibei520/p/12572487.html