gpt4 book ai didi

Javascript React 组件状态

转载 作者:行者123 更新时间:2023-11-29 20:57:45 24 4
gpt4 key购买 nike

[...Array(totalStars)] 在 React Component 下返回了什么?

它是否返回一个长度为 5 的数组,数组中的确切元素是什么?

感谢您的回答。

class StarRating extends Component {

static propTypes = {
totalStars: PropTypes.number
}

static defaultProps = {
totalStars: 5
}

constructor(props) {
super(props)
this.state = {
starsSelected: 0
}
this.change = this.change.bind(this)
}

change(starsSelected) {
this.setState({starsSelected})
}

render() {
const {totalStars} = this.props
const {starsSelected} = this.state
return (
<div className="star-rating">
{[...Array(totalStars)].map((n, i) =>
<Star key={i}
selected={i<starsSelected}
onClick={() => this.change(i+1)}
/>
)}
<p>{starsSelected} of {totalStars} stars</p>
</div>
)
}

}

最佳答案

[...Array(totalStars)] 在 React Component 下面返回了什么?

It creates an array with the length of 5 and Array has 5 undefined values, like [undefined, undefined, undefined, undefined, undefined].

它是否返回一个长度为 5 的数组,数组中的确切元素是什么?

Yes, it returns an array length of 5. In your map function return 5 star elements.

关于Javascript React 组件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48392688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com