首页 > 解决方案 > 如何使用 NextJS 检测移动设备

问题描述

有很多主题和SO问题。但我没有找到检测移动设备的最佳解决方案。

我有两个组件。第一个组件仅适用于桌面,第二个组件仅适用于手机。

{isMobile? 
(<SecondComponent />) 
: 
(<FirstComponent />)
}

大多数解决方案使用getInitialProps功能。但 Nextjs 说:

Recommended: getStaticProps or getServerSideProps.

关联

标签: javascriptreactjsnext.js

解决方案


你使用这样的东西:

export const isMobileDevice = () => /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);


推荐阅读