我的应用程序有一个警告说:Each child in an array or iterator should have a unique "key" prop. Check the render method of 'SortableTable'
听到的是我的SortableTable文件:独特的“钥匙”托警告在渲染方法 - ReactJS
import React from 'react';
import {Link} from 'react-router';
import {v4 as uuid} from 'node-uuid';
export default class SortableTable extends React.Component {
render() {
return (
<table style={{width: '100%'}} className={this.props.className} id={this.props.id}>
<thead>
<tr>
{this.props.headers.map(this.generateHeaders)}
</tr>
</thead>
<tbody>
{this.props.children}
</tbody>
</table>
);
}
generateHeaders = (value) => {
if (Object.keys(value).length === 0) return
let sort, colspan
if(value.sort) {
let {query} = this.props;
let nQuery, title, icon, colspan = 1;
if(query.sort === value.sort && query.sortDirection === 'desc') {
nQuery = Object.assign({}, query, {sort: value.sort, sortDirection: 'asc', page: 1})
title = 'asc';
icon = String.fromCharCode(0xe630)
} else {
nQuery = Object.assign({}, query, {sort: value.sort, sortDirection: 'desc', page: 1})
title = 'desc';
icon = String.fromCharCode(0xe62d)
}
sort = <Link to={this.props.link} query={nQuery} className="icon order active" title={title} data-icon={icon} />
}
let className = value.className ? value.className : ''
if(value.colspan) {
colspan = value.colspan
}
return <th className={className} colSpan={colspan}><span>{value.name}</span>{sort}</th>
}
有人能告诉我如何设置关键道具解决此警告?
感谢@Pavlo,Tom Fenech和Oleg Kutsenko(为什么我无法标记所有名称),我的问题已解决! :)) – vietanhyt
对于这个问题对未来的访问者更有帮助,请接受您最喜欢的答案(每个答案左侧都有一个勾号)。你也会从中获得一些声誉。 – Pavlo
这里所有的答案都是正确的。 :D 但我还有一个问题[与上面的SortableTable文件的同一目录中的另一个文件有关]。我不想发表一个新的问题,因为它与主要问题类似 - 缺少关键道具。我在[这里]发布了文件内容(http://textuploader.com/5i66s)请帮助我!我试图添加索引参数,但它没有工作! – vietanhyt