【发布时间】:2017-12-17 03:06:26
【问题描述】:
我试图弄清楚在数据分页时如何在 Relay Modern 应用程序中显示加载指示器。我有一个基于 PaginationContainer 的组件,我想在分页附加数据时显示加载指示器(调用 loadMore())。除了以某种方式提供自定义网络实现之外,我看不到任何其他方法可以在任何不完全是我想要的网络请求期间执行此操作。想法?
【问题讨论】:
标签: relayjs
我试图弄清楚在数据分页时如何在 Relay Modern 应用程序中显示加载指示器。我有一个基于 PaginationContainer 的组件,我想在分页附加数据时显示加载指示器(调用 loadMore())。除了以某种方式提供自定义网络实现之外,我看不到任何其他方法可以在任何不完全是我想要的网络请求期间执行此操作。想法?
【问题讨论】:
标签: relayjs
我认为 Relay 带有一个 API,可以让您知道请求是否处于待处理状态,您可以访问 this.props.relay.isLoading()(请参阅 docs)
class MyComponent extends React.Component {
render () {
return {
<div>
{this.props.relay.isLoading() ? <Spinner /> : null>
// ....
</div>
}
}
}
export default createPaginationContainer(
MyComponent,
...
)
【讨论】:
在我看来,这是一个常见的用例,应该在 Relay API 中明确支持,但以下是我能想到的最好的。鼓励提出其他解决方案的建议。
在我的基于分页容器的组件中:
constructor(arg) {
super(arg);
this.state = {isLoadingMore: false}
}
componentWillReceiveProps(nextProps) {
if (this.props !== nextProps) {
this.setState({isLoadingMore:false});
}
}
// onClick handler for "More" button
handleMore() {
if (!this.props.relay.hasMore() || this.props.relay.isLoading()) {
return;
}
this.setState({isLoadingMore: true});
this.props.relay.loadMore(
...
)
}
render() {
...
const isLoadingMore = this.state.isLoadingMore;
return (isLoadingMore ? <LoadingIndicator /> : <ListOfStuffView />
}
【讨论】: