gpt4 book ai didi

reactjs - redux 操作成功后清除表单

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

我将表单输入存储在 React 组件 state 中。当我提交表单时,我会触发 Redux 操作。当此操作成功时,我想再次更新状态 - 以清除表单。但怎么做呢?

我的意思是,我也可以轻松地将表单状态存储在 Redux 中,一切都会得到解决,但我更喜欢将组件特定的内容存储在组件状态中。

最佳答案

您应该使用类似 redux-thunk 的内容延迟调度直到 API 调用成功:

const postForm = data => dispatch => fetch(...).then((...) => dispatch(...))

由于 fetch 返回 Promise ,然后您可以等到它解决(api 调用成功)后再在组件中执行表单清除:

props.postForm(...)
.then(() => this.setState(<clear the form state>))
.catch(<do something to warn the user api call failed?>)

关于reactjs - redux 操作成功后清除表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50663189/

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