I'm trying to get this work:
Orders:
render () {
const orders = this.state.orders.map((order, index) => <OrderRow order={order} key={index}/>);
return (
<table>
<tbody>
{orders}
</tbody>
</table>
);
}
OrderRow:
render () {
return (
<tr>
<td>{this.props.order.number}</td>
<td>{this.props.order.products}</td>
<td>{this.props.order.shippingDate}</td>
<td>{this.props.order.status}</td>
</tr>
);
}
But still getting this error:
Warning: validateDOMNesting(...): <tr> cannot appear as a child of <div>.
See Orders > div > OrderRow > tr.
Any ideas how to fix it?
Wrap your OrderRow
<tr>
in <tbody>
as explained in issue here, 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>
.
OrderRow
render () {
return (
<table>
<tbody>
<tr>
<td>{this.props.order.number}</td>
<td>{this.props.order.products}</td>
<td>{this.props.order.shippingDate}</td>
<td>{this.props.order.status}</td>
</tr>
</tbody>
</table>
);
}
Something like this:
render() {
return (
<table>
<tbody>
{this.state.orders.map((order, index) => {
<tr index={index}>
<td>{order.number}</td>
<td>{order.products}</td>
<td>{order.shippingDate}</td>
<td>{order.status}</td>
</tr>
})}
</tbody>
</table>
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With