1
我的组件之一有四种不同的状态,每种状态都有自己的全屏视图(加载,错误/重试和其他2种)。React Native:如何使用耦合条件呈现大量条件视图?
现在我的渲染功能如下:
render() {
return (
<View style={{flex: 1, flexDirection: 'column'}}>
{this.renderLoadingView()}
{this.renderEmptyView()}
{this.renderErrorView()}
{this.renderInterviewListView()}
{this.renderInterviewFeedbackRequestViews()}
</View>
);
}
但每个人的这个样子,他们要么使空,如果他们不符合若干条件或他们提供如果视图所有都满足:
renderLoadingView() {
if (this.state.showLoading && !this.state.showError) {
return (
<View>
[...]
</View>
);
} else {
return null;
}
}
renderErrorView() {
if (this.state.showError) {
return (
<InterviewsErrorView onRetry={() => this.onRefresh()}/>
);
} else {
return null;
}
}
renderInterviewListView() {
var showEmpty = this.state.emptyFeedbackRequests && this.state.emptyInterviews;
if (this.state.showLoading || this.state.showError || showEmpty) {
return null;
} else if (!this.state.emptyFeedbackRequests) {
return null;
} else {
return (
<View>
[...]
</View>
);
}
}
这种感觉凌乱,尤其是因为多个视图依赖于同样的事情(如是否showLoading是真的)。有没有一种方法可以简化或使其更清洁?