首页 > 解决方案 > 如何重构我的 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))
 }

}

标签: javascriptreactjsasynchronousredux

解决方案


推荐阅读