最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why can't I nest a tbody tag inside a div tag with JSX in React? - Stack Overflow

programmeradmin6浏览0评论

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.

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

I tried putting the table inside a new ponent and then nesting the new ponent but I have the same problem. Why does JSX not like tables inside of divs? Is there any work-around?

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.

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

I tried putting the table inside a new ponent and then nesting the new ponent but I have the same problem. Why does JSX not like tables inside of divs? Is there any work-around?

Share Improve this question edited Aug 18, 2017 at 18:08 DanielK asked Aug 18, 2017 at 18:02 DanielKDanielK 31 silver badge3 bronze badges 4
  • 4 Because as the error says TBODY doesn't belong in DIV it goes inside TABLE – Patrick Evans Commented Aug 18, 2017 at 18:04
  • 1 You need to put <tbody> within a <table>. – brian17han Commented Aug 18, 2017 at 18:05
  • By the way, your question title says "table" tag while your actual question is using a "tbody" – nbkhope Commented Aug 18, 2017 at 18:06
  • Error seems to be pretty self explanatory - permitted parents of tbody: developer.mozilla/en-US/docs/Web/HTML/Element/tbody – Adam Jenkins Commented Aug 18, 2017 at 18:06
Add a ment  | 

2 Answers 2

Reset to default 8

Your problem is an HTML one. To make a table, the table body has to be inside the <table> tag:

<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>
发布评论

评论列表(0)

  1. 暂无评论