Expected server HTML to contain a matching <tr> in <table> / Hydration failed because the initial UI does not match what was rendered on the server

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

Expected server HTML to contain a matching <tr> in <table> / Hydration failed because the initial UI does not match what was rendered on the server

问题

Next.js 13.2.3

Next.js 无法使用表格元素。

"use client";
export default function index() {
  return (
    <table>
      <tr>
        <th>Company</th>
        <th>Contact</th>
        <th>Country</th>
      </tr>
      <tr>
        <td>Alfreds Futterkiste</td>
        <td>Maria Anders</td>
        <td>Germany</td>
      </tr>
      <tr>
        <td>Centro comercial Moctezuma</td>
        <td>Francisco Chang</td>
        <td>Mexico</td>
      </tr>
    </table>
  )
}

这段代码能够运行,但会出现错误。

Unhandled Runtime Error

Error: 由于初始 UI 与服务器渲染的内容不匹配,因此数据重写失败。

警告:预期服务器 HTML 包含与 &lt;table&gt; 中的 &lt;tr&gt; 匹配的内容。

我应该停止使用这个元素还是可以修复这个问题?

示例链接

英文:

Next js 13.2.3

Next js cannot use the table element.

&quot;use client&quot;;
export default function index() {
  return (
            &lt;table&gt;
                &lt;tr&gt;
                    &lt;th&gt;Company&lt;/th&gt;
                    &lt;th&gt;Contact&lt;/th&gt;
                    &lt;th&gt;Country&lt;/th&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                    &lt;td&gt;Alfreds Futterkiste&lt;/td&gt;
                    &lt;td&gt;Maria Anders&lt;/td&gt;
                    &lt;td&gt;Germany&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                    &lt;td&gt;Centro comercial Moctezuma&lt;/td&gt;
                    &lt;td&gt;Francisco Chang&lt;/td&gt;
                    &lt;td&gt;Mexico&lt;/td&gt;
                &lt;/tr&gt;
            &lt;/table&gt;
  )
}

It works but I get an error.

Unhandled Runtime Error

Error: Hydration failed because the initial UI does not match what was rendered on the server.

Warning: Expected server HTML to contain a matching &lt;tr&gt; in &lt;table&gt;.

Should I stop using this element or can I fix the problem?

https://stackblitz.com/edit/nextjs-jvjltx?file=app%2Fpage.js - example

答案1

得分: 5

这个问题已经在这里报告了:https://github.com/vercel/next.js/discussions/36754

使用theadtbody包装表格内容可以解决这个问题。

修复:https://stackblitz.com/edit/nextjs-xb5mix?file=app/page.js

英文:

This issue has been reported here: https://github.com/vercel/next.js/discussions/36754

Wrapping the table content with thead and tbody solves it.

export default function index() {
  return (
    &lt;table&gt;
      &lt;thead&gt;
        &lt;tr&gt;
          &lt;th&gt;Company&lt;/th&gt;
          &lt;th&gt;Contact&lt;/th&gt;
          &lt;th&gt;Country&lt;/th&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;Alfreds Futterkiste&lt;/td&gt;
          &lt;td&gt;Maria Anders&lt;/td&gt;
          &lt;td&gt;Germany&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td&gt;Centro comercial Moctezuma&lt;/td&gt;
          &lt;td&gt;Francisco Chang&lt;/td&gt;
          &lt;td&gt;Mexico&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  );
}

Fix: https://stackblitz.com/edit/nextjs-xb5mix?file=app/page.js

huangapple
  • 本文由 发表于 2023年3月7日 02:18:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654427.html
匿名

发表评论

匿名网友

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

确定