javascript - 如何修复 validateDOMNesting(...) : <td> cannot appear as a child of <tbody>. 和列表中的每个 child 都应该有一个唯一的 "key" Prop

标签 javascript reactjs

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

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>

我试图得到 <tbody>this.state.products.map(data=>但后来我有另一个错误
JSX expressions must have one parent element.

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

太感谢了!
祝你今天过得愉快

最佳答案

对于:Warning: validateDOMNesting(...): <td> cannot appear as a child of <tbody>. - 只需在您的 <tbody> 中添加一行

IE。
<tbody><tr><td></td></tr></tbody>
表的正确 html 结构如下所示:

引用:https://www.w3schools.com/tags/tag_tbody.asp

和:Warning: Each child in a list should have a unique "key" prop.
更改您的 map :this.state.products.map(data=>至:this.state.products.map((data, myKey)=>然后在您的 <tbody> 中使用此 key 像这样:<tbody key={myKey}>
React 组件需要一个唯一的键。在早期版本中使用 map 生成子组件时,实现者必须设置此项。最简单的方法是在您 child 的根元素中使用 map 函数中的项目索引。

引用:https://reactjs.org/docs/lists-and-keys.html

<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, myKey) =>
   <tbody key={myKey}>
     <tr>
      <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>
      </tr>
    </tbody>
  )}

</table>

有关此的更多信息:JSX expressions must have one parent element.看到这个帖子:
React - expressions must have one parent element?

关于javascript - 如何修复 validateDOMNesting(...) : <td> cannot appear as a child of <tbody>. 和列表中的每个 child 都应该有一个唯一的 "key" Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61498491/

相关文章:

javascript - 如何点击某些链接才能到达目标页面?

javascript - 当视频帖子进入/退出 ScrollView 时,Facebook 如何处理停止视频帖子?

javascript - 如何更新这个 Reactjs 选择

javascript - 无法获取数据: this.函数名不是函数(ReactJS)

javascript - Reactjs 在 for 循环结束之前不会渲染

javascript - 如何将多个文本字段传递给单个 Javascript 函数并计算结果

javascript - 在 Jquery 的每次 ajax 调用中附加我的 session ID

javascript - 在 IE 中选择禁用的单选按钮时如何保持单选按钮被选中?

javascript - ReactJS:在循环中返回 React 组件

Javascript - 如何将 html 日期限制为今天的日期?