简体   繁体   English

setstate然后在REST API中响应

[英]setstate then response in rest api react

I would like to make the state isLoading changed to true when recentTransactionsRecipient and recentTransactionsSender is not null. 我想将当recentTransactionsRecipientrecentTransactionsSender不为null时,状态isLoading更改为true。

I would like to do a promise, then 我想做个承诺

  componentDidMount() {
    this.Auth.recentTransactionsRecipient(this.props.id)
      .then(res => {
        if (res) {
          this.setState({
            recentTransactionsRecipient: res,
          });
        }
      })
      .catch(() => {
        /* just ignore */
      });

    this.Auth.recentTransactionsSender(this.props.id)
      .then(res => {
        if (res) {
          this.setState({
            recentTransactionsSender: res,
          });
        }
      })
      .catch(() => {
        /* just ignore */
      });
  }

Try using Promise.all() . 尝试使用Promise.all() See MDN doc 参见MDN文档

The Promise.all() method returns a single Promise that resolves when all of the promises passed as an iterable have resolved or when the iterable contains no promises. Promise.all()方法返回一个Promise,当作为可迭代对象传递的所有promise已解决或当可迭代对象不包含promise时,该Promise进行解析。 It rejects with the reason of the first promise that rejects. 它以第一个承诺被拒绝的理由拒绝。

Promise.all([promise1, promise2]).then((values) => {
  console.log(values);
  this.setState({
    recentTransactionsRecipient: values[0],
    recentTransactionsSender: values[1],
    isLoading: false
  });
});

The use of Promise.all() would work well here as it allows you to reduce multiple input promises into a single promise. Promise.all()的使用在这里可以很好地工作,因为它允许您将多个输入promise减少为一个promise。 The single promise returned by Promise.all() is where you would implement the isLoading : true state update logic for your component. Promise.all()返回的单个promise是您将为组件实现isLoading : true状态更新逻辑的地方。

An important detail to note with this; 需要注意的重要细节; Given your code requires any errors in Auth to be ignored (coupled with the behavior of Promise.all() to reject if any promise passed to it is rejected ), you will need to account for this in your code. 鉴于您的代码要求忽略Auth任何错误(再加上Promise.all()的行为, 如果传递给它的任何承诺 Promise.all() reject ,则将被拒绝 ),您将需要在代码中对此进行说明。

One possibility is to introduce a helper function that would "absorb" a rejected promise so that the rejected promise isn't propagated to Promise.all() (which would cause your subsequent setState() to be skipped): 一种可能性是引入一个辅助函数,该函数将“吸收”被拒绝的承诺,以使被拒绝的承诺不会传播到Promise.all() (这将导致跳过后续的setState() ):

 componentDidMount() { /* Locally defined helper method to absorb failure/rejection of the supplied promise */ const ignoreIfRejected = (promise) => { return promise().catch(() => { console.info('A promise failed. Ignoring.'); }); } /* The Promise returned from Promise.all will reject if any one of the promises in the passed array fails. Wrap each promise in locally defined ignoreIfRejected() helper to "absorb" any such error */ Promise.all([ ignoreIfRejected(this.Auth.recentTransactionsRecipient(this.props.id)), ignoreIfRejected(this.Auth.recentTransactionsSender(this.props.id)) ]) .then((recipient, sender) => { if( recipient !== null && sender !== null) { /* If resolved response from both promises returns non-null value then set isLoading state to true as required in post */ this.setState({ recentTransactionsSender : sender, recentTransactionsRecipient : recipient, isLoading : false }); } }); } 

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

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