简体   繁体   English

警告:数组或迭代器中的每个子代均应具有唯一的“键”道具。 钥匙已经存在

[英]Warning: Each child in an array or iterator should have a unique “key” prop. Keys are already present

I have a React component that accepts an array and renders it as a table using a predefined key. 我有一个React组件,它接受一个数组,并使用预定义的键将其呈现为表。

<TableFromJson data={this.props.results.events} rowKey="eventID" />

TableFromJson.js TableFromJson.js

    import React, { Component } from 'react';
    import { Table } from 'react-bootstrap';
    import PropTypes from 'prop-types';

    export class TableFromJson extends Component{

        render() {
            if (!this.props.data)
                return(<p>No data</p>);

            const key = this.props.rowKey;
            let columns = Object.keys(this.props.data[0]);
            let headerComponents = columns.map(col => <th>{col}</th>);

            let rowComponents = this.props.data.map((row) =>{
                let tdList = columns.map(col => <td>{row[col]}</td>);
                let htmlRow = <tr key={row[key]}>{tdList}</tr>;
                return htmlRow;
            });

            return (
                <Table bordered hover responsive striped>
                    <thead>
                        <tr>{headerComponents}</tr>
                    </thead>
                    <tbody>{rowComponents}</tbody>
                </Table>
            );
        }
    }

    TableFromJson.propTypes = {
        data: PropTypes.array,
        key: PropTypes.string
    }

This renders a table as show below: 这将显示一个表格,如下所示:

React Dev Tools中显示的具有键的组件

The tr element already contains the key as you can see from the above screenshot from React Dev tools. 从上面的React Dev工具截图中可以看到,tr元素已经包含了密钥。

I'm getting the key error ("Warning: Each child in an array or iterator should have a unique "key" prop") every time this component is rendered. 每次呈现此组件时,都会出现关键错误(“警告:数组或迭代器中的每个子代都应具有唯一的“关键”道具”)。 What am I missing? 我想念什么?

您还应该向子元素添加键: thtd ,正如您已经为tr所做的那样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:数组或迭代器中的每个子代都应具有唯一的“键”道具。 li中已添加的关键道具 - Warning: Each child in an array or iterator should have a unique “key” prop.; key prop already added in li 警告:数组或迭代器中的每个子代均应具有唯一的“键”道具。 检查`单位`的渲染方法 - Warning: Each child in an array or iterator should have a unique “key” prop. Check the render method of `Units` 警告:数组或迭代器中的每个子代均应具有唯一的“键”道具。 检查“搜索”的渲染方法 - Warning: Each child in an array or iterator should have a unique “key” prop. Check the render method of 'search' 警告:列表中的每个孩子都应该有一个唯一的“key”道具。 即使已经设置了密钥 - Warning: Each child in a list should have a unique "key" prop. Even after setting the key already 持久警告:“即使已经设置了密钥,数组或迭代器中的每个子节点都应该具有唯一的&#39;密钥&#39;prop” - Persistent Warning: “Each child in an array or iterator should have a unique 'key' prop” even if key is already set 数组或迭代器中的每个子代都应具有唯一的“键”道具。 反应JS错误 - Each child in an array or iterator should have a unique “key” prop. React JS Error 警告:数组或迭代器中的每个子代都应具有唯一的“键”道具 - Warning :Each child in an array or iterator should have a unique “key” prop 警告:数组或迭代器中的每个孩子都应该有一个唯一的“键”道具 - Warning: Each Child in an Array or Iterator Should Have a Unique “Key” Prop 收到警告“警告:列表中的每个孩子都应该有一个唯一的”key“道具。” 当我的组件渲染时 - Getting warning “Warning: Each child in a list should have a unique ”key“ prop.” when my component render 正在渲染对象数组的数组,不断得到“警告:列表中的每个孩子都应该有一个唯一的“键”道具。 - Was rendering arrays of arrays of objects, keep getting “Warning: Each child in a list should have a unique ”key“ prop.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM