javascript - 如何使用 NextJS 检测移动设备
问题描述
有很多主题和SO问题。但我没有找到检测移动设备的最佳解决方案。
我有两个组件。第一个组件仅适用于桌面,第二个组件仅适用于手机。
{isMobile?
(<SecondComponent />)
:
(<FirstComponent />)
}
大多数解决方案使用getInitialProps
功能。但 Nextjs 说:
Recommended: getStaticProps or getServerSideProps
.
解决方案
你使用这样的东西:
export const isMobileDevice = () => /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
推荐阅读
- javascript - pino 在 process.exit() 上丢失日志条目
- c# - 将 Windows IoT Core UWP (C# / XAML) 应用程序移植到 Linux
- docker - 在 Windows 上通过 Docker 运行 Eclipse Che 会导致来自守护进程的错误响应:OCI 运行时创建失败
- groovy - 如何在 Groovy 中使用多个 groupBy 和 max
- python - crontab 无法导入包
- php - Laravel 多对多关系多于两个数据表
- vba - 是否可以使用 VBA 代码跨 Access 数据库复制宏?
- computer-vision - 如何获得由 tesseract 读取的线坐标?
- c++ - 如何使函数能够获取指向不同类的指针?
- python - 获取字典中唯一对象的最高数字?