React: <tr> cannot appear as a child of <td>. See Comment > td > tr

情到浓时终转凉″ 提交于 2019-12-23 07:48:35

问题


So why is react complaining that I cannot have a 'tr' as a child of a 'td'?

              <tr>
                <td colSpan={2}>
                  <tr>
                    <td>
                      <Some picture>
                    </td>
                    <td>
                      <some content>
                    </td>
                  </tr>
                  <tr>
                    <td colSpan={2}>
                      <p>Paragraph stuff</p>
                    </td>
                  </tr>
                </td>
              </tr>

Maybe I have to put another table or something?


回答1:


Yes, you'll need this mark up:

<table>
    <tbody>
        <tr>
            <td colspan={2}>
                <table>
                    <tbody>
                        <tr>
                            <td></td>
                            <td></td>
                        </tr>
                        <tr>
                            <td colspan={2}>
                                <p>Paragraph stuff</p>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
    </tbody>
</table>

It is not valid markup to have a nested <tr> within a <td>. Use another table to layout it.

According to https://github.com/facebook/react/issues/5652 you will need to wrap your table contents in a tbody:

Browsers need the <tbody> tag. If it is not in your code, then the browser will automatically insert it. This will work fine on first render, but when the table gets updated, then the DOM tree is different from what React expects. This can give strange bugs, therefore React warns you to insert the <tbody>. It is a really helpful warning.

Thanks @Stefan Dragnev



来源:https://stackoverflow.com/questions/41716528/react-tr-cannot-appear-as-a-child-of-td-see-comment-td-tr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!