Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with "Warning: validateDOMNesting(...): <tr> cannot appear as a child of <div>. " when using react-window to render table rows

I'm using react-window to create virtual tables with react-table 7 (and material UI tables).

I'm embedding FixedSizeList instead TableBody. Something like this:

   <TableBody {...getTableBodyProps()}>
    <FixedSizeList
      height={listHeight}
      itemCount={rows.length}
      itemSize={rowHeight}
    >
     {RenderRow}
    </FixedSizeList>)
   </TableBody>

and RenderRow returns the TableRows. Something like this:

 const RenderRow = React.useCallback(  ({ index, style }) =>
 {
     const row = rows[index];
     prepareRow(row);
     const rowProps = row.getRowProps();
     return (<TableRow
              {...row.getRowProps({
               style,
              })} />);
 }

Because of how react-window works, it creates a couple of divs to implement the list scrolling, dynamically embedding the needed TableRows as required, causing a react js warning to be output.

webpack-internal:///490:506 Warning: validateDOMNesting(...): <tr> cannot appear as a child of <div>

Just ignoring this warning, isn't something I want to do, as it may cause other warning not to be noticed. (nor do I want to use a release build while testing)

So is it possible to either prevent this warning from being emitted? Or is it possible to use react-window for table rows, without getting this warning?

Update: Trying the setting innerElementType to tbody suggestion.

This changes the inner div that FixedSizeList renders.

from:

<div style="position: relative; height: 96px; overflow: auto; will-change: transform; direction: ltr;">
<div style="height: 96px; width: 100%;">

to

<div style="position: relative; height: 96px; overflow: auto; will-change: transform; direction: ltr;">
<tbody style="height: 96px; width: 100%;">

So the are now included inside tbody.

So I guess I also need to use outerElementType to change the outer div, to deal with the div in table warning, but I can't think of anything valid that will work...

If I didn't want to include a thead I could set outerElementType to table and innerElementType to tbody

like image 813
Tom Avatar asked Sep 11 '20 21:09

Tom


People also ask

How to fix “<tr> cannot appear as a child of <Div>” in react?

To fix the “<tr> cannot appear as a child of <div>” error in React, we should remove the wrapper div. For instance, we can write: to create the Rows array, which has the JSX for the tr elements that we created by mapping them from the data array with map .

How to fix the warning validatedomnesting (…) error when developing react apps?

" Error When Developing React Apps To fix the "Warning: validateDOMNesting (…): <a> cannot appear as a descendant of <a>. " error when developing React apps, we should make sure we don’t nest a elements within a elements. Browsers will fix the HTML when the code is rendered, which will make React’s virtual DOM different from what’s rendered.

Can validatedomnesting be used as a child of <Div>?

validateDOMNesting (...): cannot appear as a child of <div> . I see an error similar to this You replace : tag <body> to <div> Show activity on this post.


Video Answer


1 Answers

FixedSizeList accepts an innerElementType prop to let you specify the HTML tag to use instead of div. As far as I can tell from reading the code, it more or less needs to be a tag string.

You'd probably want this innerElementType to be tbody, which would mean re-working the parent elements a bit; I'm guessing you would not want to continue using TableBody.

like image 121
backtick Avatar answered Nov 10 '22 06:11

backtick