简体   繁体   English

持久警告:“即使已经设置了密钥,数组或迭代器中的每个子节点都应该具有唯一的'密钥'prop”

[英]Persistent Warning: “Each child in an array or iterator should have a unique 'key' prop” even if key is already set

I've been trying to figure about the warning that react had been showing. 我一直试图想出反应已经显示的警告。 Clearly I have the tags set with key yet the warning still insists. 显然,我已经使用密钥设置了标签,但警告仍然存在。

render() {
    return (
        <div className="table">
            <table>
                <tbody>
                    {Object.entries(this.props.rowData).map(rowData => 
                        <tr key={rowData.id}> 
                            {Object.entries(rowData).map((data, index) =>
                                <td key={index}> Test </td>
                            )}
                        </tr>
                    )}
                </tbody>
            </table>
        </div>
    );
}

It seems that you are misunderstanding what Object.entries does. 您似乎误解了Object.entries作用。

Object.entries returns an array of tuples (array of 2 elements) for each key/value pair it contains: Object.entries为它包含的每个键/值对返回一个元组数组(包含2个元素的数组):

 // logs [["id", 5], ["name", "example"]] console.log( Object.entries( { id: 5, name: 'example' } ) ) 

What you probably want is Object.values which will return an array of values from an object. 你可能想要的是Object.values ,它将从一个对象返回一个值数组。

 // logs [5, 'example'] console.log( Object.values( { id: 5, name: 'example' } ) ) 

EDIT: 编辑:

Based on the data you provided, rowData is an array so there's no need to use any Object.* methods on it. 根据您提供的数据rowData是一个数组,因此不需要使用任何Object.*方法。 We can just map on it directly: 我们可以直接映射它:

render() {
    return (
        <div className="table">
            <table>
                <tbody>
                    {this.props.rowData.map(row => 
                        <tr key={row.id}> 
                            {Object.entries(row).map(([key,value], index) =>
                                <td key={index}> {key},{value} </td>
                            )}
                        </tr>
                    )}
                </tbody>
            </table>
        </div>
    );
}

暂无
暂无

声明:本站的技术帖子网页,遵循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 警告:数组或迭代器中的每个孩子都应该有一个唯一的“键”道具 - 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. Keys are already present 警告:列表中的每个孩子都应该有一个唯一的“key”道具。 即使已经设置了密钥 - Warning: Each child in a list should have a unique "key" prop. Even after setting the key already 数组或迭代器中的每个孩子都应该有一个唯一的“key” prop - Each child in an array or iterator should have a unique "key" prop 数组或迭代器中的每个子代在reactjs中都应具有唯一的“键”道具 - Each child in an array or iterator should have a unique “key” prop in reactjs React - 数组或迭代器中的每个子节点都应该有一个唯一的“key”prop - React - Each child in an array or iterator should have a unique “key” prop React JS:警告:数组或迭代器中的每个子代都应具有唯一的“键”属性 - React JS : 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. Check the render method of `Units`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM