關(guān)于渲染5顆星的問(wèn)題
我在constructor里邊定義了一個(gè)totalStars為星的總數(shù)量,
this.state = {
totalStars: 5
}
當(dāng)我去循環(huán)出所有星星的時(shí)候,這樣寫的
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>
)
}
為什么會(huì)報(bào)錯(cuò)呢
2018-11-02
你的totalStars不是數(shù)組,只是一個(gè)數(shù)字5而已。改成[1,2,3,4,5]