【发布时间】:2016-06-17 15:47:40
【问题描述】:
我有一个自定义的 material-ui 组件,定义如下。
import React, { PropTypes } from 'react';
import Paper from 'material-ui/lib/paper';
function PaperBox (props) {
return (
<Paper className={props.layout}>
<div className="row center-xs">
<p>{props.box.title}</p>
</div>
<div className="row center-xs">
<p className="col-xs-10">
{props.box.text}
</p>
</div>
</Paper>
);
}
PaperBox.propTypes = {
box: PropTypes.shape({
title: PropTypes.string.isRequired,
text: PropTypes.any.isRequired
}).isRequired,
layout: PropTypes.string.isRequired
};
export default PaperBox;
在另一个文件中,我有一个父组件,它将一组文本对象映射到上述组件,如下所示
import React from 'react';
import PaperBox from '../shared/PaperBox/PaperBox';
const layout = 'col-md-3 col-xs-8';
function DetailContent (props) {
return (
<div className="row around-xs">
{props.boxes.map(box => <PaperBox key={box.title} box={box} layout={layout} />)}
</div>
);
}
export default DetailContent;
你可以想象,“props.boxes”是一个文本对象数组
const boxes = [
{
title: 'one',
text: 'I am a bunch of text'
},
{
title: 'two',
text: 'I am also a bunch of text'
},
];
对我来说非常奇怪的是,虽然我在 PaperBox 组件中添加了 key 属性,但它仍然会打印 Warning: Each child in an array or iterator should have a unique "key"道具。 在控制台中。有解决办法吗?
非常感谢
【问题讨论】:
-
标题是否保证唯一?
-
@DavidL.Walsh,我将密钥更改为迭代器索引,但错误仍然存在
-
@Theo 那里的 OP 遇到了与我非常相似的问题,但我的自定义子组件中没有任何迭代器。
-
这里是原始仓库:github.com/lorix-lpan/perfect-schedule,希望这有助于解决问题。谢谢!