【问题标题】:Error: Each child in a list should have a unique "key" prop错误:列表中的每个孩子都应该有一个唯一的“关键”道具
【发布时间】:2019-12-11 05:09:27
【问题描述】:

我正在研究 React-Map-GL,我对它非常陌生。我已将此代码添加到我的 react-app 中,但收到如下错误:

它使用:“eslint-config-airbnb”

提前谢谢你

警告:列表中的每个孩子都应该有一个唯一的“key”属性。

    import React from 'react'
    import { Collapse, Descriptions } from 'antd'
    import styles from './style.module.scss'

    import data from '../data.json'

    const { Panel } = Collapse

    function callback(key) {
      console.log(key)
    }

    class InboxPacks extends React.Component {
      state = {
        inboxPackages: data.inboxPackages,
      }

      render() {
        const { inboxPackages } = this.state
        return (
          <div>
            <Collapse bordered={false} onChange={callback} className={styles.inbox}>
              {inboxPackages.map((item, index) => (
                <Panel
                  key={index.toString()}
                  header={[<span>{item.name}</span>, <small>{item.received}</small>]}
                  extra={[
                    <span>{item.weight} lb</span>,
                    <small>
                      {item.dimensions} {`in`}
                    </small>,
                  ]}
                >
                  <Descriptions layout="vertical" className={styles.descriptionsPanel}>
                    <Descriptions.Item label="Courier" className={styles.volkan}>
                      {item.courier}
                    </Descriptions.Item>
                    <Descriptions.Item label="Tracking Number">{item.tracking}</Descriptions.Item>
                    <Descriptions.Item label="Storage Left">{item.storageLeft}</Descriptions.Item>
                    <Descriptions.Item label="Customs Value">{`$${item.customsValue}`}</Descriptions.Item>
                    <Descriptions.Item label="Content">{item.content}</Descriptions.Item>
                  </Descriptions>
                </Panel>
              ))}
            </Collapse>
          </div>
        )
      }
    }

    export default InboxPacks

【问题讨论】:

  • 从你的错误日志来看,问题似乎来自你的Panel(第28行)的额外道具中的span,你可以尝试添加一个key道具吗?还是道具不应该包含数组?
  • 我认为您需要key 才能获得Descriptions

标签: reactjs


【解决方案1】:

这与 headerextra 属性有关 - 如您所见,它们是数组,请尝试将 key 属性添加到数组中的每个元素。

试试这个:

import React from 'react'
import { Collapse, Descriptions } from 'antd'
import styles from './style.module.scss'

import data from '../data.json'

const { Panel } = Collapse

function callback(key) {
  console.log(key)
}

class InboxPacks extends React.Component {
  state = {
    inboxPackages: data.inboxPackages,
  }

  render() {
    const { inboxPackages } = this.state
    return (
        <div>
          <Collapse bordered={false} onChange={callback} className={styles.inbox}>
            {inboxPackages.map((item, index) => (
                <Panel
                    key={`panel_${index}`}
                    header={[<span key={`header_span_${index}`}>{item.name}</span>, <small key={`header_small_${index}`}>{item.received}</small>]}
                    extra={[
                      <span key={`extra_span_${index}`}>{item.weight} lb</span>,
                      <small key={`extra_small_${index}`}>
                        {item.dimensions} {`in`}
                      </small>,
                    ]}
                >
                  <Descriptions layout="vertical" className={styles.descriptionsPanel}>
                    <Descriptions.Item label="Courier" className={styles.volkan}>
                      {item.courier}
                    </Descriptions.Item>
                    <Descriptions.Item label="Tracking Number">{item.tracking}</Descriptions.Item>
                    <Descriptions.Item label="Storage Left">{item.storageLeft}</Descriptions.Item>
                    <Descriptions.Item label="Customs Value">{`$${item.customsValue}`}</Descriptions.Item>
                    <Descriptions.Item label="Content">{item.content}</Descriptions.Item>
                  </Descriptions>
                </Panel>
            ))}
          </Collapse>
        </div>
    )
  }
}

export default InboxPacks

【讨论】:

  • 我该怎么做?
  • 您可以为数组中的每个元素添加属性键 - 因为您使用的是 map 我建议将迭代索引加入键字符串。
  • 非常感谢您的回复。我收到以下错误:./src/pages/inbox/InboxPacks/index.js 第 27 行:不要在键中使用数组索引 react/no-array-index-key 第 28 行:不要在键中使用数组索引 react/ no-array-index-key 第 31 行:不要在键中使用数组索引 react/no-array-index-key 第 32 行:不要在键中使用数组索引 react/no-array-index-key 搜索关键字详细了解每个错误。
  • 太好了,我也更新了我的答案来解决这个问题:)
  • :(( 看起来一样。/src/pages/inbox/InboxPacks/index.js 第 25 行:不要在键中使用数组索引 react/no-array-index-key 第 27 行:做不要在键中使用数组索引 react/no-array-index-key 第 28 行:不要在键中使用数组索引 react/no-array-index-key 第 31 行:不要在键中使用数组索引 react/no-array- index-key 第 32 行:Do not use Array index in keys react/no-array-index-key 搜索关键字以了解每个错误的更多信息。
猜你喜欢
  • 2020-08-10
  • 2021-09-03
  • 1970-01-01
  • 2019-08-21
  • 2021-09-23
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 2020-01-24
相关资源
最近更新 更多