我有一個呈現標記的組件。它通過一個Map循環並顯示數據。我嘗試使用forEach,但它不起作用。然而,如果我將地圖轉換爲數組(foreach不能在數組上工作),它會起作用。我在這裏錯過了什麼?反應js組件,地圖工程,foreach不
這工作:
render(){
return(
<div class="container">
{Array.from(this.props.tags.values()).map((tag,i) => (
<Tag
handleKeyDown={this.handleKeyDown.bind(this)}
handleBlur={this.handleBlur.bind(this)}
handleTouchTap={this.handleTouchTap.bind(this)}
handleRequestDelete={this.handleRequestDelete.bind(this)}
tag={tag}
key={i}
/>
))}
</div>
)
}
這不:
render(){
return(
<div class="container">
{this.props.tags.forEach((tag,i) => (
<Tag
handleKeyDown={this.handleKeyDown.bind(this)}
handleBlur={this.handleBlur.bind(this)}
handleTouchTap={this.handleTouchTap.bind(this)}
handleRequestDelete={this.handleRequestDelete.bind(this)}
tag={tag}
key={i}
/>
))}
</div>
)
}
謝謝!除了將地圖轉換爲數組並使用地圖之外,還有更好的方法嗎? – Gopid
當然,@Gopid,我用一個使用生成器的例子更新了答案,它可以避免迭代一次以轉換爲數組,並且可以直接迭代並將條目映射到新格式。 –