简体   繁体   English

在this.props.relay.setVariables触发fetch之后加载指标?

[英]Loading indicator after this.props.relay.setVariables triggered fetch?

I use react-router-relay and within one of my routes I have a dropdown. 我使用react-router-relay ,在我的一条路线中,我有一个下拉列表。

Now I select a different value from that dropdown - that will call this.props.relay.setVariables which will fetch some data based on this variables. 现在我从该下拉列表中选择一个不同的值 - 这将调用this.props.relay.setVariables ,它将根据此变量获取一些数据。 This data will be displayed in a chart component within the current route. 此数据将显示在当前路径中的图表组件中。

How can I provide a callback or something to know that the current component is fetching data now / finished fetching data now ? 如何提供回调或某些东西,以便知道当前组件现在正在获取数据/现在获取数据?

I need this to be able to show a loading indicator on top of the chart component. 我需要这个能够在图表组件的顶部显示加载指示器。

I think I can't use react-router-relay for this since it would only enable me to show a loading indicator instead of my current route - but I want to show the loading indicator within the current route. 我想我不能使用react-router-relay ,因为它只能让我显示一个加载指示器而不是我当前的路由 - 但我想在当前路由中显示加载指示器。

Please help. 请帮忙。

The setVariables method takes a second argument, an onReadyStateChange callback, that can be used to detect when a request is pending and when it has completed or failed. setVariables方法接受第二个参数,一个onReadyStateChange回调,可用于检测请求何时挂起以及何时完成或失败。

See also the setVariables docs and the guide to ready states - basically the readyState is an object of {aborted, ready, done, error, ...} which you can use to update the UI appropriately: 另请参阅setVariables文档就绪状态指南 - readyState基本上是{aborted, ready, done, error, ...} ,您可以使用它来适当地更新UI:

  this.props.relay.setVariables({...}, readyState => {
    if (readyState.done || readyState.aborted) {
      this.setState({loading: false});
    } else if (readyState.error) {
      this.setState({loading: false, error});
    }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM