【发布时间】:2017-03-10 08:13:15
【问题描述】:
有时所有 this.states 都由单独的函数初始化 Reactjs 中的构造函数。我可以知道为什么吗?
例如:
class Articles extends React.Component {
constructor(props) {
super(props);
this.initializer();
}
initializer(){
this.state = {
selectedArticle: this.props.article[0],
showArticleDetails: true,
showArticleContent: false
};
}
}
之后,this.initializer(); 在我们需要的地方使用。
【问题讨论】:
-
您还应该将 props 作为参数传递并使用它而不是
this.propsbtw。 -
谢谢你,但我不能准确地得到你。你能在这里简单地写一些代码吗?
-
他的意思是你应该将 props 作为初始化器的参数传入:
constructor(props) { super(props); this.initializer(props); } initializer(props){ this.state = { selectedArticle: props.article[0], showArticleDetails: true, showArticleContent: false }; } -
谢谢你们..你们俩的解释都很好..
标签: reactjs react-router react-jsx reactjs-flux