简体   繁体   English

唯一键是唯一的,已分配给 key prop,但仍然出现错误……但仅在一个组件中

[英]unique keys are unique, are assigned to key prop, but still getting error… but only in one component

within my main App render function... I have this list generated.在我的主应用程序中呈现 function... 我生成了这个列表。

    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}

        />)
    });

but I still get the console output error, even though each key is both defined and unique....但我仍然得到控制台 output 错误,即使每个键都是定义和唯一的......

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)

weirder still I have a similar loop for other things, where key is not acting weird.更奇怪的是,我对其他事情也有类似的循环,其中 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. 

        />)
    });   

and that output is:并且 output 是:

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

so I don't understand why the above isn't working while in the same page the below is.所以我不明白为什么上面的内容在下面的同一页面中不起作用。

can anyone shed any light as to what may be happening?任何人都可以阐明可能发生的事情吗?

edit: in case it is relevant, the building component编辑:如果相关,建筑组件

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}
}


Your error is indeed in building.您的错误确实在构建中。 You are not adding keys to the PriceList.您没有向价目表添加键。 This needs a key这需要一把钥匙

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

The error it gave you was pointing you in the correct place它给你的错误是把你指向正确的地方

As indicated by the error message, the problem is within your <Building> component:如错误消息所示,问题出在您的<Building>组件中:

Check the render method of Building .检查Building的渲染方法。

It even tells you where:它甚至告诉你在哪里:

in div (at Building.js:11)在 div 中(在 Building.js:11)

Valiant debugging efforts though!尽管进行了英勇的调试工作!

eta: If your buyPrice array isn't going to change, the fix is straightforward: eta:如果你的buyPrice数组不会改变,修复很简单:

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

暂无
暂无

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

相关问题 当我分配了一个键时,为什么会出现“列表中的每个子项都应该有一个唯一的“键”道具错误”? - Why am I getting 'Each child in a list should have a unique "key" prop error' When I have a key assigned? React Checkbox Component返回唯一键道具错误 - React Checkbox Component returns unique key prop error 列表中的每个孩子都应该有一个唯一的“关键”道具。 我做到了,但仍然看到这个错误 - Each child in a list should have a unique "key" prop. I made it, but still see this error 我已经输入了一个独特的“关键”道具,但仍然有错误要求我在 React 中输入它 - I have input a unique "key" prop but still got error asking me to input it in React 反应独特的关键道具 - React unique key prop 反应:独特的“关键”道具 - React: unique “key” prop 收到警告“警告:列表中的每个孩子都应该有一个唯一的”key“道具。” 当我的组件渲染时 - Getting warning “Warning: Each child in a list should have a unique ”key“ prop.” when my component render 反应递归手风琴组件抛出错误列表中的每个孩子都应该有一个唯一的关键道具 - react recursive accordion component throwing an error each child in a list should have a unique key prop 使用唯一键道具解决问题 - Fixing the problem with a unique keys prop 如何使用功能组件修复 ReactJS 中唯一的“关键”道具和 validateDOMNesting(...) - How to fix unique "key" prop and validateDOMNesting(...) in ReactJS with fuctional component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM