關于渲染5顆星的問題
我在constructor里邊定義了一個totalStars為星的總數量,
this.state = {
totalStars: 5
}
當我去循環出所有星星的時候,這樣寫的
renderStars() {
return (
<div className="stars-area">
{
this.state.totalStars.map((item,index) => {
return <span key={index} className={this.state.stars >= item ? "red-star" : ''}>★</span>
})
}? ? ? ??
</div>
)
}
為什么會報錯呢
2018-11-02
你的totalStars不是數組,只是一個數字5而已。改成[1,2,3,4,5]