我目前正在学习 reactJS,我发现很难理解为什么 reactjs 中的箭头函数有时被视为组件,有时被视为普通箭头函数。
在这个例子中:
class CommentListContainer extends React.Component {
state = { comments : [] };
componentDidMount() {
fetchSomeComments(s => // 1- here fetchSomeComments is treated as a normal arrow function
this.setState({ comments: s }));
}
render() {
return <CommentList comments={this.state.comments} />; // 2- here CommentList is treated as a component
}
}
// 1
const fetchSomeComments = cb =>
cb([
{ author: "Chan", body: "You look nice today." },
{ author: "You", body: "I know, right?!" }
]);
// 2
const CommentList = comments =>
<ul>
{
comments.comments.map(
(c, index) => (
<li key = {index}>{c.body}—{c.author}</li>
)
)
}
</ul>
我想理解这一点,如果 CommentList 是一个组件,它怎么能写成一个带有构造函数(props)的类?
三国纷争
繁星点点滴滴
相关分类