首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何修复validateDOMNesting(...):<td>不能作为<tbody>的子级出现。列表中的每个子元素都应该有一个唯一的"key“道具

如何修复validateDOMNesting(...):<td>不能作为<tbody>的子级出现。列表中的每个子元素都应该有一个唯一的"key“道具
EN

Stack Overflow用户
提问于 2020-04-29 17:26:42
回答 2查看 7K关注 0票数 5

我正在尝试修复这两个警告:

代码语言:javascript
运行
复制
Warning: validateDOMNesting(...): <td> cannot appear as a child of <tbody>.

Warning: Each child in a list should have a unique "key" prop.

下面是我的表js文件:

代码语言:javascript
运行
复制
        <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>,但是我又遇到了另一个错误

代码语言:javascript
运行
复制
JSX expressions must have one parent element.

任何帮助或想法来解决这个问题都将不胜感激。

非常感谢!祝您今天愉快

EN

Stack Overflow用户

发布于 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>标记中。最后,您的代码应该如下所示:

代码语言:javascript
运行
复制
<thead>
<tr>
<th>
//other content here like <td>
</th>
</tr>
</thead>
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61498491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档