[英]Unique “key” prop missing from components with key
I am getting the following error in console 我在控制台中收到以下错误
warning.js:33 Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `SectionList`. See for more information.
in div (created by SectionList)
in SectionList (created by Sections)
in div (created by Sections)
in div (created by Sections)
in Sections (created by SeatingChart)
in div (created by SeatingChart)
in div (created by SeatingChart)
in div (created by SeatingChart)
in div (created by SeatingChart)
in SeatingChart (created by Connect(SeatingChart))
I have a SeatingChart
component 我有一个
SeatingChart
组件
render() {
return (
<div className="container-fluid">
<div className="row">
<div className="col-md-12">
<div className="col-md-4 offset-md-1 align-top" style={SeatingChartStyle.sectionBuilder}>
<Sections teamName={this.props.seatingChart.teamName} sections={this.props.seatingChart.sections} saveSection={this.saveSection}/>
</div>
</div>
</div>
</div>
);
}
which renders the Sections
component 呈现
Sections
组件
renderSectionList(){
if(this.props.teamName){
return (
<div>
<br />
<h3>Sections</h3>
<SectionList sections={this.props.sections} saveSection={this.props.saveSection} />
</div>
);
}
}
render() {
return (
<div>
{this.renderSectionList()}
</div>
);
}
which renders the SectionsList
component 呈现
SectionsList
组件
previewSection(item, index){
return (
<div>
<SectionItem key={index} section={item} saveSection={this.props.saveSection}/>
</div>
);
}
sectionListMap() {
if(this.props.sections) {
let sections = Object.values(this.props.sections);
return (
sections.map(this.previewSection)
);
}
}
render() {
return (
<div className="col-md-9">
<div id="accordion" role="tablist" aria-multiselectable="true">
<SectionItem key={-1} section={sectionObj} saveSection={this.props.saveSection} />
{this.sectionListMap()}
</div>
</div>
);
}
Where am I missing my key, each SectionItem
has a key prop. 我在哪里缺少钥匙,每个
SectionItem
都有一个钥匙道具。
warning.js:33 Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `SectionList`. See for more information.
in **div** (created by SectionList)
You're rendering a list of div
(containing one SectionItem
each) and your div
do not have a key. 您正在呈现
div
列表(每个列表包含一个SectionItem
),并且div
没有键。 You can fix the error by moving the key
prop to your div
ie 您可以通过将
key
道具移至div
来修正错误,即
previewSection(item, index){
return (
<div key={index}>
<SectionItem section={item} saveSection={this.props.saveSection}/>
</div>
);
}
In this case SectionItem
doesn't require a key because there's only 1 SectionItem per div as opposed to a list. 在这种情况下,
SectionItem
不需要键,因为每个div(而不是列表)只有1个SectionItem。
Or if you don't need the div, then removing it and keeping the key on SectionItem
will also fix the error 或者,如果您不需要div,则将其删除并将键保留在
SectionItem
也可以修复错误
previewSection(item, index){
return (
<SectionItem key={index} section={item} saveSection={this.props.saveSection}/>
);
}
In JavaScript: the Array.map()
function returns an array. 在JavaScript中:
Array.map()
函数返回一个数组。 In your case: you want to return an array of react components. 在您的情况下:您想返回一组react组件。 React wants to see the key at the top level of each component.
React希望在每个组件的顶层看到键。
Therefore, you need to expose the key at the top level of your previewSection()
function. 因此,您需要将键公开在
previewSection()
函数的顶层。
To achieve this: I would remove the superfluous "div" element to render the component as follows: 要实现此目的:我将删除多余的“ div”元素以按如下所示呈现组件:
previewSection(item, index){
return (
<SectionItem key={index} section={item} saveSection={this.props.saveSection}/>
);
}
previewSection(item, index){
return (
<div>
<SectionItem key={index} section={item} saveSection={this.props.saveSection}/>
</div>
);
}
Your keys should not be numeric. 您的密钥不能为数字。 So rather than
index
, try something like {`section${index}`}
因此,与其
index
,不如尝试{`section${index}`}
Even better, the keys should not be based on an order index at all. 更好的是,键根本不应该基于顺序索引。 So if your
sectionObj
has a property like a unique id, that would be ideal to use. 因此,如果您的
sectionObj
具有诸如唯一ID之类的属性,则非常适合使用。
What helped me was using uuid in the key prop. 帮助我的是在关键道具中使用了uuid 。
In your case: 在您的情况下:
import uuid from "uuid";
render() {
return (
<div className="col-md-9">
<div id="accordion" role="tablist" aria-multiselectable="true">
<SectionItem key={uuid()} section={sectionObj} saveSection={this.props.saveSection} />
{this.sectionListMap()}
</div>
</div>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.