isMobile
使用库中的属性react-device-detect
进行条件渲染时会发生此错误。
为了解决这个错误,我使用创建了一个新的下一个应用程序npx create-next-app
,然后react-device-detect
使用 npm 安装了该软件包。然后我只是用这个替换了 index.js:
import {
BrowserView,
MobileView
} from 'react-device-detect'
export default function Home() {
return <div>
<BrowserView>Hello</BrowserView>
<MobileView>World</MobileView>
</div>
}
当我启动开发服务器并在桌面上打开页面时,我看到 Hello 并且没有错误。但是当我在手机上使用 Safari 时,我看到了 World,但我可以在开发工具中看到我也收到以下警告消息:
Warning: Text content did not match. Server: "Hello" Client: "World"
div
MobileView
div
Home
MyApp
PureComponent
ReactDevOverlay
_classCallCheck
AppContainer
Root
我读过一些关于水合作用的文章,发现我可以添加 suppressHydrationWarning 属性来抑制这个警告,但这是我应该在这里做的,还是我不明白什么?