我正在使用react-rating-starts-component,我在使用.map的卡片中多次使用它,它工作得很好。然而,当我将它添加到一个页面时,我给它的初始值是0,它没有从这个值中改变。我使用后端在componentdidmount()中更新了它的值,在控制台中它给了我5,但它显示为0
获取星星并查看它们的函数
function getstars(props){
return(
<ReactStars
value={props.Rating}
count={5}
size={25}
edit={false}
isHalf={false}
activeColor="#ffea00"
/>
)
}来自组件的SetState
this.setState({
info: resp.data.PlaceInformation,
id: resp.data.PlaceInformation._id,
Name: resp.data.PlaceInformation.Name,
Description: resp.data.PlaceInformation.Description,
Phone: resp.data.PlaceInformation.Phone,
Website: resp.data.PlaceInformation.Website,
Instagram: resp.data.PlaceInformation.Instgram,
Twitter: resp.data.PlaceInformation.Twitter,
Rating: resp.data.PlaceInformation.Rating,
PRating: resp.data.PlaceInformation.PRating,
OpenTime: resp.data.PlaceInformation.OpenTime,
CloseTime: resp.data.PlaceInformation.CloseTime,
Image: resp.data.PlaceInformation.icon,
Photos: resp.data.PlaceInformation.photos,
GoogleMap: resp.data.PlaceLocation.GoogleMap,
City: resp.data.PlaceLocation.City,
Street: resp.data.PlaceLocation.Street,
ZibCode: resp.data.PlaceLocation.ZibCode,
Reviews: resp.data.PlaceReviews,
})在react渲染中
<div>{getstars(this.state.Rating)}</div>额定值的初始值是"0“,它在启动时不会改变,但在控制台中会改变。当我改变rating的初始值时,它就会改变。感谢您帮助我并考虑到这一点:)
发布于 2021-06-06 13:11:26
我认为你需要检查一下你的论点。在函数getStars中,您传递的是this.state.Rating对象,而不是整个状态,所以当您在内部使用它时,我想您应该直接使用它,而不是从props获取它。
<div>{getstars(this.state.Rating)}</div>
//You passed the whole Rating object here代码应该是
function getstars(Rating){
return(
<ReactStars
value={Rating}
count={5}
size={25}
edit={false}
isHalf={false}
activeColor="#ffea00"
/>
)
}或者,您可以更改传递的方式,并使用对象文字传递它,这样就可以在props参数中获取Rating。
<div>{getstars({Rating: this.state.Rating } )}</div>
//Here you wrap the Rating with object literal
//so that in the function you can fetch it from props argument这样就不需要更改getStars函数了。
https://stackoverflow.com/questions/67855942
复制相似问题