在Next.js 13中添加了函数到`useEffect`后仍然存在水合错误。

huangapple go评论71阅读模式
英文:

Hydration error even after adding the fuction to the useEffect in Next JS 13

问题

使用下面的代码创建一个导航菜单,显示当前日期和时间,尽管组件是客户端组件,并且使用了 useEffect:

### /src/components/navigation/index.tsx

'使用客户端'

import Link from 'next/link'
import React, { useEffect, useState } from 'react'

export default function Navigation() {
  // 时间
  const [time, setTime] = useState(new Date().toLocaleTimeString())

  useEffect(() => {
    const locale = 'en'

    const updateFormattedTime = () => {
      const today = new Date()
      const formattedTime = today.toLocaleTimeString(locale, {
        day: 'numeric',
        month: 'numeric',
        year: 'numeric',
        hour: 'numeric',
        hour12: true,
        minute: 'numeric',
        timeZone: 'Asia/Kolkata'
      })
      setTime(formattedTime)
    }
    updateFormattedTime()

    const timer = setInterval(() => {
      updateFormattedTime()
    }, 60000)

    return () => {
      clearInterval(timer)
    }
  }, [])

  return (
    <div className="navigation">
      <Link href="/">
        OUTDATED
      </Link>
      <Link href="/information">
        Information
      </Link>
      {time && <p className="information red">{time.replace(/\//g, ',')}</p>}
    </div>
  )
}

它基本上只是获取日期和时间并将其推送到状态,然后在标题本身显示它,并且只是用逗号替换斜杠。

确切的错误消息:

错误:文本内容与服务器呈现的 HTML 不匹配。
警告:文本内容不匹配。服务器端:&quot;5:43:50 PM&quot; 客户端:&quot;5:43:51 PM&quot;
详细信息请参见:https://nextjs.org/docs/messages/react-hydration-error
英文:

Using next 13 and making a navigation menu which shows the current date and time and I'm getting hydration error although the component is a client component and also Is using useEffect

### /src/components/navigation/index.tsx

&#39;use client&#39;

import Link from &#39;next/link&#39;
import React, { useEffect, useState } from &#39;react&#39;

export default function Navigation() {
  // Time
  const [time, setTime] = useState(new Date().toLocaleTimeString())

  useEffect(() =&gt; {
    const locale = &#39;en&#39;

    const updateFormattedTime = () =&gt; {
      const today = new Date()
      const formattedTime = today.toLocaleTimeString(locale, {
        day: &#39;numeric&#39;,
        month: &#39;numeric&#39;,
        year: &#39;numeric&#39;,
        hour: &#39;numeric&#39;,
        hour12: true,
        minute: &#39;numeric&#39;,
        timeZone: &#39;Asia/Kolkata&#39;
      })
      setTime(formattedTime)
    }
    updateFormattedTime()

    const timer = setInterval(() =&gt; {
      updateFormattedTime()
    }, 60000)

    return () =&gt; {
      clearInterval(timer)
    }
  }, [])

  return (
    &lt;div className=&quot;navigation&quot;&gt;
      &lt;Link href=&quot;/&quot; className=&quot;head animate&quot;&gt;
        OUTDATED
      &lt;/Link&gt;
      &lt;Link href=&quot;/information&quot; className=&quot;head animate&quot;&gt;
        Information
      &lt;/Link&gt;
      {time &amp;&amp; &lt;p className=&quot;information red&quot;&gt;{time.replace(/\//g, &#39;,&#39;)}&lt;/p&gt;}
    &lt;/div&gt;
  )
}

It basically just get the date and time and pushes it to a state and shows it on the the header itself. and I just replace the slash with commas

The extact error message

Error: Text content does not match server-rendered HTML.
Warning: Text content did not match. Server: &quot;5:43:50 PM&quot; Client: &quot;5:43:51 PM&quot;
See more info here: https://nextjs.org/docs/messages/react-hydration-error

答案1

得分: 0

尝试使用suppressHydrationWarning,这应该可以解决水合问题。

英文:

Try suppressHydrationWarning, this should fix hydration issue

答案2

得分: 0

水合作用会导致组件渲染两次。一次在预渲染中,然后再在浏览器渲染中。这会导致您的 setInterval 运行两次。

解决这个问题的一种方法是在导入该组件时禁用 ssr

import dynamic from 'next/dynamic'
const Navigation = dynamic(() => import('@/components/navigation'), {
  ssr: false,
})
英文:

Hydration causes the component to render twice. Once in the pre-render and then again in the browser render. Causing your setInterval to run twice.

One way around this is to disable ssr for this component where you import it:

import dynamic from &#39;next/dynamic&#39;
const Navigation = dynamic(() =&gt; import(&#39;@/components/navigation&#39;), {
ssr: false,
})

huangapple
  • 本文由 发表于 2023年6月22日 20:11:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定