相关疑难解决方法(0)

了解React.js中数组子项的唯一键

我正在构建一个React组件,它接受JSON数据源并创建一个可排序的表.
每个动态数据行都有一个分配给它的唯一键,但我仍然收到以下错误:

数组中的每个子节点都应该具有唯一的"键"支柱.
检查TableComponent的render方法.

我的TableComponentrender方法返回:

<table>
  <thead key="thead">
    <TableHeader columns={columnNames}/>
  </thead>
  <tbody key="tbody">
    { rows }
  </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

TableHeader组件是单行,并且还具有分配给它的唯一键.

每个rowin rows都是使用具有唯一键的组件构建的:

<TableRowItem key={item.id} data={item} columns={columnNames}/>
Run Code Online (Sandbox Code Playgroud)

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>
    )
  }
});
Run Code Online (Sandbox Code Playgroud)

是什么导致了唯一的关键道具错误?

javascript reactjs

549
推荐指数
9
解决办法
48万
查看次数

在React中获取关键道具警告,即使设置了关键

问题

我收到这个警告:

警告:数组或迭代器中的每个子节点都应该具有唯一的"键"支柱.检查EventsTable的render方法.有关更多信息,请参见fb.me/react-warning-keys.

react-runtime-dev.js?8fefd85d334323f8baa58410bac59b2a7f426ea7:21998警告:数组或迭代器中的每个子节点都应该有一个唯一的"key"prop.检查Event的render方法.有关更多信息,请参见fb.me/react-warning-keys.

资源

这是EventsTable:

EventsTable = React.createClass({
  displayName: 'EventsTable',

  render() {
    console.dir(this.props.list);

    return (
      <table className="events-table">
        <thead>
          <tr>
            {_.keys(this.props.list[0]).map(function (key) {
              if (key !== 'attributes') {
                return <th>{key}</th>;
              }
            })}
         </tr>
        </thead>

        <tbody>
          {this.props.list.map(function (row) {
            return (
              <Event key={row.WhatId} data={row} />
            );
          })}
        </tbody>
      </table>
    )
  }
});
Run Code Online (Sandbox Code Playgroud)

这是Event:

Event = React.createClass({
  displayName: 'Event',

  render() {
    return (
      <tr>
        {_.keys(this.props.data).map((x) => {
          if (x !== 'attributes')
            return <td>{this.props.data[x]}</td>;
        })} …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

27
推荐指数
4
解决办法
4万
查看次数

标签 统计

javascript ×2

reactjs ×2