【问题标题】:Stop React JS / Flux application loading until one API request is done?停止 React JS / Flux 应用程序加载,直到完成一个 API 请求?
【发布时间】:2016-04-07 14:56:04
【问题描述】:

我正在使用带有 Alt (Flux) 库的 React JS。

一开始我需要从 REST API 获取用户数据。当这个请求不成功时,我不想渲染整个应用程序并显示一些错误。如果成功获取数据,则应继续渲染。我使用Axios库来获取数据。

如何实现这个取数据测试?

【问题讨论】:

  • 只需执行一个普通的旧 axios 请求并将您的反应渲染包装在成功回调中。
  • 好像axios不支持同步请求。

标签: javascript reactjs flux


【解决方案1】:

这里有一个解决方案,它先进行调用,然后仅在调用成功时才呈现您的应用。

axios.get('url')
  .then(function (response) {
    // success, let's render the app
    // you can pass the whole response object or just the data to your app
    ReactDOM.render(
        <MyApp reesponse={response}/>,
        document.getElementById('app')
    );
  })
  .catch(function (response) {
    // something went wrong
  });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    相关资源
    最近更新 更多