简体   繁体   English

为什么我不能在 React 中使用 JSX 将 tbody 标签嵌套在 div 标签中?

[英]Why can't I nest a tbody tag inside a div tag with JSX in React?

I'm a React beginner and I'm working on a project where I'll have a form and as data gets entered a table will be dynamically populated with data from the user.我是一个 React 初学者,我正在做一个项目,在这个项目中我将有一个表单,当输入数据时,一个表将动态填充来自用户的数据。

My code:我的代码:

<div>
  <tbody>
    <tr>
      <td>{this.props.someData}</td>
    </tr>
    <tr>
      <td>{this.props.moreData}</td>
    </tr>
  </tbody>
</div>

React is throwing this error: ValidateDOMNesting(...): cannot appear as a child of div React 抛出此错误:ValidateDOMNesting(...): 不能作为 div 的子项出现

I tried putting the table inside a new component and then nesting the new component but I have the same problem.我尝试将表放在新组件中,然后嵌套新组件,但我遇到了同样的问题。 Why does JSX not like tables inside of divs?为什么 JSX 不喜欢 div 中的表格? Is there any work-around?有什么解决办法吗?

Your problem is an HTML one.您的问题是 HTML 问题。 To make a table, the table body has to be inside the <table> tag:要制作表格,表格主体必须位于<table>标签内:

<table>
  <tbody>
  ...
  </tbody>
...
</table>

No table tag works without tag.没有标签,任何表格标签都不起作用。 should be under like :应该如下:

<table>
<tbody>
    <tr>
      <td>{this.props.someData}</td>
    </tr>
    <tr>
      <td>{this.props.moreData}</td>
    </tr>
  </tbody>
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM