gpt4 book ai didi

javascript - 如何为两个 redux 操作创建 async/await 结构?

转载 作者:行者123 更新时间:2023-12-01 00:14:39 26 4
gpt4 key购买 nike

我对按钮更改功能有两个相互依赖的操作。我想要做的是将这两个函数放入 async/await 结构中,以便在 update_other_filter 操作结束后,我将能够运行 getTotalData 操作。像下面的结构一样运行它实际上不会以正确的方式更新状态。我正在 getTotaldata 中发送先前的状态(在 update_other_filter 之前)。

你们可能会说,当它解析时,我必须在 update_other_filter 操作中分派(dispatch) getTotalData。但在我的项目的这种状态下,我似乎无法改变任何事情。我不太擅长 async/await 和 Promise 概念,所以我只想在我的 React 组件中创建 async/await 函数,而不是在 onChange 函数中调用它。有办法做到这一点吗?

onChange = {(event) => {
this.props.setSpinner()
//this update filter function updates filter which will be sent to server in getTotalData action
this.props.update_other_filter(true,"website",!event.target.checked)
//this action should wait for update_other_filter to end than it has correct parameters to send to server
this.props.getTotalData(this.props.totalFilters, apiUrl)
}

最佳答案

async onChange = {(event) => {
this.props.setSpinner()
await this.props.update_other_filter(true,"website",!event.target.checked)
this.props.getTotalData(this.props.totalFilters, apiUrl)
}

关于javascript - 如何为两个 redux 操作创建 async/await 结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59853111/

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