解决Next.js服务端渲染时出现Hydration(水合)错误

错误信息(hydration-error-info.js:67  Warning: Expected server HTML to contain a matching <span> in <div>.)忽略错误依然可以正常运行,但是每次刷新后都会显示这个错误。

hydration-error-info.js:67 
 Warning: Expected server HTML to contain a matching <span> in <div>..
...........
react-dom.development.js:12507  Uncaught Error: Hydration failed because the initial UI does not match what was rendered on the server.
..........
Warning: An error occurred during hydration. The server HTML was replaced with client content in <div>. 
See more info here: https://nextjs.org/docs/messages/react-hydration-error
........
react-dom.development.js:19878  Uncaught Error: There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering.

导致这个错误的原因是因为我们在页面上显示的数据没有使用useState

而在页面上直接使用

解决就是直接使用useState就不会报错了

 useEffect(() => {
    setUserData(user)  //使用userData代替user

    /**
    * .......
    */
  }, [userData]);

参考文章:解决Next.js服务端渲染时出现Hydration(水合)错误在Next.js服务端渲染的时候,难免会出现Hydrat - 掘金

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值