我正在尝试修复这两个警告:
Warning: validateDOMNesting(...): <td> cannot appear as a child of <tbody>.
Warning: Each child in a list should have a unique "key" prop.
下面是我的表js文件:
<table className="table table-hover">
<thead>
<tr>
<th scope="col"style={{width: "10%"}}>ID</th>
<th scope="col"style={{width: "10%"}}>Name</th>
<th scope="col"style={{width: "10%"}}>Price $</th>
<th scope="col" style={{width: "10%"}}>Quantity</th>
<th scope="col" style={{width: "10%"}}>Total $:</th>
<th scope="col" style={{width: "10%"}}>Total €:</th>
<th scope="col" style={{width: "20%"}}>Remove:</th>
<th scope="col" style={{width: "20%"}}>Change Quantity:</th>
</tr>
</thead>
{this.state.products.map(data=>
<tbody>
<td>{data.id}</td>
<td>{data.name}</td>
<td>{data.price}</td>
<td>{data.quantity}</td>
<td>{data.quantity*data.price}</td>
<td>{Math.round(data.quantity*data.price*0.92)}</td>
<td>
<button type="button" onClick={(e)=>this.handleSubmitRemove(data.id)} className="btn btn-danger">Remove</button>
</td>
<td>
<button style={{margin: "3px"}} type="button" onClick={(e)=> this.updateCart(data.id,1)}>+</button>
<button style={{margin: "3px"}} disabled={data.quantity==1} type="button" onClick={(e)=> this.updateCart(data.id,-1)}>-</button>
</td>
</tbody>
)}
</table>
我试图在this.state.products.map(data=>
之外获取<tbody>
,但是我又遇到了另一个错误
JSX expressions must have one parent element.
任何帮助或想法来解决这个问题都将不胜感激。
非常感谢!祝您今天愉快
发布于 2020-12-30 00:44:10
我遇到了一个错误,上面写着:
How to fix validateDOMNesting(…): <th> cannot appear as a child of <thead>. and Each child in a list should have a unique “key” prop
。
我终于发现我错过了<thead></thead>
标签之后的<tr></tr>
。因此,请检查您的<thead></thead>
标记,并查看其他<th></th>
标记是否包含在<tr></tr>
标记中。最后,您的代码应该如下所示:
<thead>
<tr>
<th>
//other content here like <td>
</th>
</tr>
</thead>
https://stackoverflow.com/questions/61498491
复制相似问题