Reactjs 如何为react组件定义正确的初始状态?
例如,一个ArticleList组件,起初没有数据 州政府可能会这样:Reactjs 如何为react组件定义正确的初始状态?,reactjs,redux,Reactjs,Redux,例如,一个ArticleList组件,起初没有数据 州政府可能会这样: 状态={ 物品清单:[], isFetching:错误 }您可以使用 this.state={}; 初始化状态 然后必须按照文档中的说明使用此.setState() 在es6中,您可以使用 variable === undefined 及 检查变量 state = { articleList : [], fetching: false } render() { const {fetching, arti
状态={
物品清单:[],
isFetching:错误
}
您可以使用
this.state={};
初始化状态
然后必须按照文档中的说明使用此.setState()
在es6中,您可以使用
variable === undefined
及
检查变量
state = {
articleList : [],
fetching: false
}
render() {
const {fetching, articleList} = this.state;
if(fetching) {
//show loader here
return loader;
}
if(!fetching && articleList) {
if(articleList.length === 0) {
//show message here
return message;
}
//render content here
return content;
}
return null;
}
state = {
articleList : [],
fetching: false
}
render() {
const {fetching, articleList} = this.state;
if(fetching) {
//show loader here
return loader;
}
if(!fetching && articleList) {
if(articleList.length === 0) {
//show message here
return message;
}
//render content here
return content;
}
return null;
}