我正在构建一个React组件,它接受JSON数据源并创建一个可排序的表。
每个动态数据行都有一个唯一的键分配给它,但我仍然得到一个错误:
数组中的每个子元素都应该有一个唯一的“key”道具。
检查TableComponent的渲染方法。
我的TableComponent渲染方法返回:
<table>
<thead key="thead">
<TableHeader columns={columnNames}/>
</thead>
<tbody key="tbody">
{ rows }
</tbody>
</table>
TableHeader组件是单行,也有一个唯一的键赋给它。
行中的每一行都是由一个具有唯一键的组件构建的:
<TableRowItem key={item.id} data={item} columns={columnNames}/>
TableRowItem看起来是这样的:
var TableRowItem = React.createClass({
render: function() {
var td = function() {
return this.props.columns.map(function(c) {
return <td key={this.props.data[c]}>{this.props.data[c]}</td>;
}, this);
}.bind(this);
return (
<tr>{ td(this.props.item) }</tr>
)
}
});
是什么导致唯一键道具错误?
根据React文档,每一行/项都应该有一个唯一的键。
键帮助React识别哪些项已经更改、添加或删除。
就我个人而言,我更喜欢使用加密接口来生成一个随机UUID:
(crypto在vanilla-js中是内置的)
const numbers = [1, 2, 3, 4, 5];
const listItems = numbers.map((number) =>
<li key={crypto.randomUUID()}>item {number}
</li>
);
我认为在处理表(或类似的例子)时,为了重用性,应该将创建的唯一键从父组件传递给子组件。
因为如果你正在创建一个表,这意味着你正在从父表传递数据。如果你赋值key={row.name},可能当前数据有name属性,但如果你想在其他地方使用这个表组件,你假设在你传递的每一行数据中,你都有name属性。
由于工程师将在父组件中准备数据,因此工程师应该基于这些数据创建一个键函数。
const keyFunc = (student) => {
return student.id;
};
在这种情况下,工程师知道它正在发送什么数据,它知道每一行都有唯一的id属性。也许在不同的数据集中,数据集是股票价格它没有id属性,只有符号
const keyFunc = (stock) => {
return stock.symbol;
};
这个keyFunc应该作为道具传递给子组件,以保证可重用性和唯一性。
如果我们有数组对象数据。然后我们绘制地图来显示数据。并传递唯一的id (key = {product。Id})因为浏览器可以选择唯一的数据。
example : [
{
"id": "1",
"name": "walton glass door",
"suplier": "walton group",
"price": "50000",
"quantity": "25",
"description":"Walton Refrigerator is the Best Refrigerator brand in bv
Bangladesh "
},
{
"id": "2",
"name": "walton glass door",
"suplier": "walton group",
"price": "40000",
"quantity": "5",
"description":"Walton Refrigerator is the Best Refrigerator brand in
Bangladesh "
},
}
现在我们映射数据并传递唯一id:
{
products.map(product => <product product={product} key={product.id}
</product>)
}
当你创建一个没有特殊key属性的元素列表时,React中会出现“列表中的每个子元素都应该有一个唯一的“key”道具”警告。必须为循环中的每个元素分配键,以便为React中的元素提供稳定的标识。
我们可以将对象的id属性设置为唯一键。
export default function App() {
const posts = [
{ id: 1, title: "First "},
{ id: 2, title: "Second" },
{ id: 3, title: "Third" }
];
return (
<div>
<ul>
{posts.map(value =>
<li key={value.id}>{value.title}</li>
)}
</ul>
</div>
);}
//simple way
//if u using ant design remove the empty fragment...
//worng ans---> change to following crt ans
export default function App() {
const posts = [
{ id: 1, title: "First "},
{ id: 2, title: "Second" },
{ id: 3, title: "Third" }
];
{fields.map((field,index)=>{
return(
<> //empty fragment
<Row key={index}>
<Col span={6}>hello</Col>
</Row>
</>
)
})}
//correct ans
//remove the empty fragments after solve this key.prop warning problem
export default function App() {
const posts = [
{ id: 1, title: "First "},
{ id: 2, title: "Second" },
{ id: 3, title: "Third" }
];
{fields.map((field,index)=>{
return(
<> //empty fragment
<Row key={index}>
<Col span={6}>hello</Col>
</Row>
</>
)
})}
列表中的每个子元素都应该有一个唯一的“key”道具。
通过在呈现元素中的key属性中声明index值来解决。
App.js组件
import Map1 from './Map1';
const arr = [1,2,3,4,5];
const App = () => {
return (
<>
<Map1 numb={arr} />
</>
)
}
export default App
Map.js组件
const Map1 = (props) => {
let itemTwo = props.numb;
let itemlist = itemTwo.map((item,index) => <li key={index}>{item}</li>)
return (
<>
<ul>
<li style={liStyle}>{itemlist}</li>
</ul>
</>
)
}
export default Map1