【发布时间】:2016-12-27 17:56:41
【问题描述】:
我收到关于数组中每个孩子都需要密钥的警告。我之前也遇到过这个问题,在Coffeescript中已经解决了:"Each child in an array should have a unique key prop" only on first time render of page
我知道我必须通过map 传递密钥,这样map 调用的每个数组都会收到一个唯一的密钥。在 Coffeescript 中,我可以这样做:
component1 = React.createClass({
render: () ->
_.chain(@state.users).map((x) -> <component2 profile={x} key={x.id} />),@).value()
)}
component2 = React.createClass({
render: () ->
return (
<div>Test</div>
<div>Test</div>
)
})
我尝试在 Javascript 中执行此操作,而不是调用新组件,我只是在同一个组件中调用另一个函数。我仍然收到警告:
export default class About extends React.Component {
aboutMe(item) {
return (
<div className="col-xs-12">
<div className="about-body">
<p>{item.description}</p>
</div>
</div>
)
}
render() {
return (
<div className="container">
<div className="row">
<div className="col-xs-9">
{_.chain(this.props.about).map(this.aboutMe).value()} # How would I pass in a key in this `map`?
</div>
</div>
</div>
)
}
【问题讨论】:
-
那么
this.aboutMe是什么,看起来您将它用作函数,并且您可能想要.map( x => x.aboutMe )或类似的东西。
标签: javascript coffeescript jsx