Next.JS + Sanity. 在包括时间戳时出现水合错误

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

Next.JS + Sanity. Hydration Error when including Time Stamp

问题

Here's the translated code portion:

有人对此有答案吗?每当我包括从Sanity获取的时间戳时,我都会收到一个水合错误。页面上的表达方式如下:

         <p className="font-extralight text-sm">
              由 <span className="text-blue-500">{post.author.name}</span>{" "}
              发布于 {new Date(post._createdAt).toLocaleString()}
            </p>

页面似乎可以正常呈现,但水合错误令人讨厌。删除此行会消除水合错误。如果有帮助的话,我也在此应用程序上使用了SSR和ISR。
英文:

Anyone have an answer to this? I am receiving a hydration error whenever I include a timestamp fetched from Sanity. Here's the way it is expressed on the page.

     <p className="font-extralight text-sm">
          Post by <span className="text-blue-500">{post.author.name}</span>{" "}
          Published at {new Date(post._createdAt).toLocaleString()}
        </p>

The page seems to render fine but the hydration error is annoying. Removing this line gets rid of the hydration error. I am using SSR and ISR on the this app as well if that is helpful.

答案1

得分: 1

只需将 suppressHydrationWarning 属性添加到呈现可能导致不匹配的元素中,例如在您的情况下是日期。

它仅适用于一级深度。不要过度使用它,它旨在用作紧急逃生通道。

文档中更多信息

英文:

Just add suppressHydrationWarning property to the element which renders something that might produce mismatch, like a date in your case.

It only works one level deep. Don’t overuse it, it is intended to be used as an escape hatch.

More in the docs

huangapple
  • 本文由 发表于 2023年4月11日 03:06:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979956.html
匿名

发表评论

匿名网友

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

确定