I'm having problems dynamically adding jsx table rows to a table in react.
The case I have is the following:
If a table row contains sub rows, add them in addition to the main row.
This was my initial design:
{rows.map((row) => {
const expanded = _.includes(expandedRows, row.name); // check if row is expanded or not
const subRows = row.subRows && row.subRows.map((subRow) =>
<SubRow key={subRow.name} theme={theme} subRow={subRow.name} />);
return (
<div>
<Row
key={row.name}
theme={theme}
handleUpdate={handleUpdate}
handleExpandOrCollapseRow={handleExpandOrCollapseRow}
/>
{expanded && row.subRows && subRows}
{expanded && !row.subRows && <p>No subrows exist</p>}
</div>
);
})
The problem is that div is not allowed as child in the tbody tag. Any ideas how I can get around this without wrapping my return in a div?
I was thinking to move the whole tbody to my Row component and conditionally render the subrows there, but I can't see how it would help since I still can't use any wrapper around the return... the only wrapper I can use is tbody and that can only occur once as parent to all rows.
Any idea?
tbodyinstead of adivand remove thetbodyfrom the parent ? - Olivier Boisséresult.add(<Row ...>)thenresult.add(subrow)for each subrows - Olivier Boissé