我收到有關陣列中每個需要密鑰的孩子的警告。我以前就遇到過這一點,我在CoffeeScript的解決了這個問題:"Each child in an array should have a unique key prop" only on first time render of page通過Javascript中的映射函數傳遞鍵值
我知道,我必須通過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)'或類似的東西。 – adeneo