Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 476
pizzae Avatar asked Jan 18 '17 10:01

pizzae


1 Answers

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

like image 94
janhartmann Avatar answered Oct 23 '22 01:10

janhartmann