最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Each child in an array or iterator should have a unique "key" prop - Stack Overflow

programmeradmin5浏览0评论

was getting the warning message () in the below code using react.js. I checked answers on the stackoverflow and tried to remove the warning message but it didn't help. Made a separate example with similar functionality in a static page and it is working fine. But, this code is throwing warning message. How to remove this warning message ?

<tbody>
      {list.map(function(value){
             return(<tr className="gradeA" role="row">
                        <td className="sorting_1">{ value.id }</td>
                        <td>{value.name}</td>                          
                        <td>{value.location}</td>
                     </tr>);                                        
            })
        }       
</tbody>

was getting the warning message () in the below code using react.js. I checked answers on the stackoverflow and tried to remove the warning message but it didn't help. Made a separate example with similar functionality in a static page and it is working fine. But, this code is throwing warning message. How to remove this warning message ?

<tbody>
      {list.map(function(value){
             return(<tr className="gradeA" role="row">
                        <td className="sorting_1">{ value.id }</td>
                        <td>{value.name}</td>                          
                        <td>{value.location}</td>
                     </tr>);                                        
            })
        }       
</tbody>
Share Improve this question asked Sep 26, 2016 at 8:33 WorkerWorker 2831 gold badge9 silver badges20 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

You just need to add a unique key to the returned ponent from map. In your map function receive define another parameter as key and for each tr that you return just add key={key} as a prop.

<tbody>
      {list.map(function(value, key){
             return(<tr className="gradeA" role="row" key={key}>
                        <td className="sorting_1">{ value.id }</td>
                        <td>{value.name}</td>                          
                        <td>{value.location}</td>
                     </tr>);                                        
            })
        }       
</tbody>
发布评论

评论列表(0)

  1. 暂无评论