我正在构建一个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条件&&先映射,在我已经添加了用户id键,以确保它是唯一的

 <tbody>
                                    {users &&
                                    users.map((user) => {
                                        return <tr key={user._id}>
                                            <td>{user.username}</td>
                                            <td><input
                                                name="isGoing"
                                                type="checkbox"
                                                checked={user.isEnabled}
                                                onChange={handleInputChangeNew}/></td>
                                            <td>{user.role.roleTitle} - {user.role.department.departmentName}</td>
                                            {/*<td className="text-right">
                                                    <Button>
                                                        ACTION
                                                    </Button>
                                                </td>*/}
                                        </tr>
                                    })
                                    }


                                    </tbody>

其他回答

您的密钥应该是唯一的。就像一个唯一的id。你的代码应该是这样的

<div>
 {products.map(product => (
   <Product key={product.id}>
    </Product>
    ))}
  </div>

react中定义唯一键的最佳解决方案: 在映射内部,初始化名称post,然后用key={post定义键。Id}或者在我的代码中,你看到我定义了名称item,然后我定义key by key={item. Id}:

< div className = "容器" > {职位。地图(项= > ( <div className="card border-primary mb-3" key={item.id}> . < div className = " card-header " > {item.name} < / div > <div className="card-body" > < h4 className = " card-title " > {item.username} < / h4 > < p className = "卡片文本" > {item.email} < / p > < / div > < / div > )} < / div >

如果我们有数组对象数据。然后我们绘制地图来显示数据。并传递唯一的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>)
}

我有一个唯一的键,只需要像这样传递它作为道具:

<CompName key={msg._id} message={msg} />

这个页面很有帮助:

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

我不做详细的解释,但这个答案的关键是“关键” 只需将key属性放在标签中,并确保每次迭代时都赋予它唯一的值

#确保键的值不与其他值冲突

例子

<div>
        {conversation.map(item => (
          <div key={item.id  } id={item.id}>
          </div>
        ))}
      </div>

conversation是一个数组,如下所示:

  const conversation = [{id:"unique"+0,label:"OPEN"},{id:"unique"+1,label:"RESOLVED"},{id:"unique"+2,label:"ARCHIVED"},
   ]