【问题标题】:unique keys are unique, are assigned to key prop, but still getting error... but only in one component唯一键是唯一的,已分配给 key prop,但仍然出现错误...但仅在一个组件中
【发布时间】:2025-11-24 01:45:01
【问题描述】:

在我的主 App 渲染函数中...我生成了这个列表。

    const buildings = this.state.buildings.map(buildingData=>
      {
       console.log("unique id is:", buildingData.id) 
      return(
        <Building 
          key={buildingData.id}
          name ={buildingData.name}
          handleBuildingBuy={this.handleBuildingBuy}
          buyPrice={buildingData.buyPrice}
          count = {buildingData.count}
          subjectsOfIncrease = {buildingData.subjectsOfIncrease}
          isBuyable = {buildingData.isBuyable}
          isUnlocked = {buildingData.isUnlocked}

        />)
    });

但我仍然收到控制台输出错误,即使每个键都是已定义且唯一的......

App.js:224 unique id is: 0
App.js:224 unique id is: 1
App.js:224 unique id is: 2
App.js:224 unique id is: 3
App.js:224 unique id is: 4
App.js:224 unique id is: 5
App.js:224 unique id is: 6
App.js:224 unique id is: 7
index.js:1 Warning: Each child in a list should have a unique "key" prop.

Check the render method of `Building`. See react-warning-keys for more information.
    in div (at Building.js:11)
    in Building (at App.js:226)
    in div (at App.js:275)
    in div (at App.js:274)
    in App (at src/index.js:8)

更奇怪的是,我对其他事情也有类似的循环,其中 key 的行为并不奇怪。

    const resources = this.state.resources.map(resourceData=>
      {
       console.log("unique resourceName is:", resourceData.name) 

        return(

        <Resource 
          name ={resourceData.name}
          max = {resourceData.max}
          isUnlocked = {resourceData.isUnlocked}
          changePerTick = {resourceData.changePerTick}
          amount={resourceData.amount}
          key={resourceData.name} //change out to a number - names are in fact unique. 

        />)
    });   

那个输出是:

unique resourceName is: grey
App.js:240 unique resourceName is: red
App.js:240 unique resourceName is: green
App.js:240 unique resourceName is: blue
App.js:240 unique resourceName is: purple
App.js:240 unique resourceName is: teal
App.js:240 unique resourceName is: orange

所以我不明白为什么上面的内容在同一页面中不起作用。

任何人都可以阐明可能发生的事情吗?

编辑:如果它是相关的,建筑组件

function Building(props)
    {
    if(props.isUnlocked)
        {
        const buyClass = props.isBuyable ? "afford": "cantAfford";  
        const PriceList = props.buyPrice.map(price=>
            {
            return(
                <div>
                    {price.name}: {price.cost}  
                </div>
            )
        })  

        return(

        <div className="building">
            <div className="buildingTitle"> BUY FOR:

                <div className={buyClass}>{PriceList}</div> 

            </div>
             <div className="buildingBuyContainer">
                {props.isBuyable ? 
                    <button name={props.name} onClick={props.handleBuildingBuy} className="buildingBuyBTN">{props.name}</button>
                    :
                    <button name={props.name} className="buildingNoBuyBTN">{props.name}</button>
                }
            </div>
            <div className="counter"> COUNT:{props.count}
        </div>
        </div>

        )
    }
    else{return null}
}


【问题讨论】:

  • 问题出在&lt;Building/&gt;(第 11 行)内 - 请参阅错误消息:Check the render method of 'Building'.
  • MB。我没有仔细阅读您的代码。
  • 你能发布更多的 building.js 吗?错误指向的第 11 行发生了什么?

标签: javascript reactjs list key


【解决方案1】:

您的错误确实在构建中。您没有向价目表添加键。这需要一把钥匙

      const PriceList = props.buyPrice.map(price=>
            {
            return(
                <div>
                    {price.name}: {price.cost}  
                </div>
            )
        })  

它给你的错误是把你指向正确的地方

【讨论】:

  • 是的...我是哑巴或盲人。映射任何地方都需要密钥,对于一切......这是我应该深入脑海的东西。
  • 哟 - 在别人已经发布后重复别人的答案是不好的形式
  • 不管老兄,我们都知道发生了什么。
  • 我看不到如何重述他在问题中发布的错误作为解决方案,但我离题了
【解决方案2】:

如错误消息所示,问题出在您的&lt;Building&gt; 组件中:

查看Building的渲染方法。

它甚至告诉你在哪里:

在 div 中(在 Building.js:11)

尽管进行了英勇的调试工作!

eta:如果您的 buyPrice 数组不会更改,那么修复很简单:

const PriceList = props.buyPrice.map((price,index)=>
            {
            return(
                <div key={index}>
                    {price.name}: {price.cost}  
                </div>
            )
        })  

【讨论】:

  • @altruios,您不接受这个答案有什么原因吗?
最近更新 更多