javascript - 如何重构我的 POST fetch 函数以与 Redux 更加异步?
问题描述
我对将 react 与 redux 一起使用是相当新的,尤其是在使某些功能异步时。只是对有人会采取的步骤感到好奇。下面是我当前的 POST 函数,其中添加了一些 Redux 代码。
createCheckList = (title) => {
if (!(title === '')) {
fetch('http://localhost:3001/api/v1/check_lists', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({ check_list: {title: title }})})
.then((resp) => resp.json())
.then(data => {
this.props.dispatch(addCheckList(data.id, data.title))
})
.catch(error => console.log(error))
}
}
解决方案
推荐阅读
- javascript - javascrpit - if 语句不起作用。没有将数字与变量对齐
- c# - 需要apiversioning版本的asp net core中间件
- c# - 在 C# 中解析十进制类型意外失败
- r - 列中特定字符串的计数
- php - 无法访问受保护的属性参数 laravel 5.5
- javascript - 从javascript数组中提取具有相同值的项目组
- r - 如何在表中没有观察到的情况下抑制变量级别?
- go - go mod 找不到依赖的版本 v0.0.0-00010101000000-000000000000
- ssl - 需要使用 https、jwt 和 bcrypt 确认安全性
- java - 使用 Spring Data JPA 在本机 SQL 查询中使用命名参数