Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

React warns that a div sits directly inside a tbody

ErrorWarning: validateDOMNesting(...): <div> cannot appear as a child of <tbody>.

Short answer

HTML only lets a tbody contain tr elements, plus script-supporting elements. A div directly inside it is invalid, so React warns. Put a tr there, and put the content in a td. For a message that spans the whole table, use one td with colSpan. Check wrapper components that render a div inside table markup.

  • Applies to React 19, Bootstrap 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    The warning prints a component stack. Read it from the top to find the component that renders the div, then open that file.

    grep -rn "<tbody" src/
  2. 2Match what you see, then jump to its fix

    1. 1The JSX has <tbody><div>...</div></tbody> or a map that returns divsA div is written directly inside tbodyFix
    2. 2The component stack shows a custom component between tbody and the divA component renders a div, such as an empty-state or loading messageFix

What does the error look like?

  • Warning: validateDOMNesting(...): <div> cannot appear as a child of <tbody>.

Cause 1 of 2

A div is written directly inside tbody

1Check

The HTML specification gives the content model of tbody as zero or more tr elements and script-supporting elements. A div is neither.

grep -rn -A2 "<tbody" src/ | grep "<div"

2Fix

Return rows, and put the content into cells.

<tbody>
  {items.map(item => (
    <tr key={item.id}>
      <td>{item.name}</td>
    </tr>
  ))}
</tbody>

3Verify

Reload the page and check that the console warning is gone.

# browser console: no validateDOMNesting message

Confirmed by html.spec.whatwg.org

Did this fix it?

Cause 2 of 2

A component renders a div, such as an empty-state or loading message

1Check

A custom component can hide the div. A td may sit only directly inside a tr, and tr may hold td, th and script-supporting elements, so a message must be a row with a cell.

grep -rn "No data\|Loading" src/ | head

2Fix

Render a row with one cell that spans all columns, using colSpan.

<tbody>
  <tr>
    <td colSpan={4}>No results found.</td>
  </tr>
</tbody>

3Verify

Show the empty state and check that the console is clean.

# browser console: no validateDOMNesting message

Confirmed by html.spec.whatwg.org

Did this fix it?

How do I stop it coming back?

  • Treat table, thead, tbody and tr as fixed structure: only rows in tbody, only cells in tr.
  • Keep a wrapper div outside the table, not inside it.
  • Open the console after changing a table component.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.