[英]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.