gpt4 book ai didi

javascript - setstate 然后在 Rest api React 中响应

转载 作者:行者123 更新时间:2023-12-03 00:13:47 25 4
gpt4 key购买 nike

recentTransactionsRecipientrecentTransactionsSender 不为 null 时,我希望将状态 isLoading 更改为 true。

那么我愿意做一个 promise

  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 */
});
}

最佳答案

尝试使用Promise.all()See MDN doc

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. 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
});
});

关于javascript - setstate 然后在 Rest api React 中响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54604880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com