reactjs - redux 操作成功后清除表单
问题描述
我将表单输入存储在 React componentstate
中。当我提交表单时,我触发了一个 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?>)
推荐阅读
- javascript - 动态渲染来自两个数组的信息
- lua - 对表数组进行排序
- java - 在 .jar 包中找不到 Java / Mvn 本地资源
- javascript - 用 javascript 编写的数字时钟代码在 HTML 中显示错误的时间和偏移量
- android - 如何为我的整个颤振应用程序提供线性渐变?
- java - 没有合格的 bean - FxWeaver 和 Spring Boot
- regex - 正则表达式位于路径中间的 Nginx 位置
- angular - 使用 Jasmine 和 Karma 进行角度单元测试时出错
- javascript - nuxtjs + express,为什么返回html?
- azure - 通过 Azure API 管理下载 Azure Blob 文件失败并出现错误 - BackendConnectionFailure: at transfer-response