Api 如何在react with redux、redux thunk和axios中设置post请求的回调?

Api 如何在react with redux、redux thunk和axios中设置post请求的回调?,api,reactjs,redux,axios,redux-thunk,Api,Reactjs,Redux,Axios,Redux Thunk,我正在尝试执行异步post请求,以仅在成功后更新数据 我想要的是在行动呼叫中使用承诺类型的东西 this.props.postAction('/event/user/44/post',data) .then((response) =>{ console.log("Inside callback"); console.log(response); }); export default connect({postAction})(EventFeed); 这是我的行动文件,我在那里提

我正在尝试执行异步post请求,以仅在成功后更新数据

我想要的是在行动呼叫中使用承诺类型的东西

this.props.postAction('/event/user/44/post',data)
.then((response) =>{
  console.log("Inside callback"); 
  console.log(response);
});
export default connect({postAction})(EventFeed);
这是我的行动文件,我在那里提出post请求

export const POST_ACTION = 'POST_ACTION';

export function postAction(url,data) {
  return function(dispatch) {
      axios({
          method:'post',
          url:API_URL + url,
          headers: {'event': 55},
          data: data,
      })
    .then(response => {
      dispatch({
        type: POST_ACTION,
        payload: response.data
      });
    })
    .catch((error) => {
      console.log(error);
    })
  }
}
但我再也进不去了


现在,如何进行回调以从响应中获取数据?

首先,您不应该这样做。你违反了背后的整个想法。当使用Redux时,异步内容应该单独在action creator中处理,它使用潜在的数据负载分派操作,存储得到更新,应用程序重新呈现。当你通过一个承诺链伸出手来“回头”时,你就违反了这个流程

有了这个警告,你就不会从你的恶棍那里得到回报。当链接then时,您还应该返回您的响应


对于这类问题,也许是一个更好的地方。

首先,你不应该。你违反了背后的整个想法。当使用Redux时,异步内容应该单独在action creator中处理,它使用潜在的数据负载分派操作,存储得到更新,应用程序重新呈现。当你通过一个承诺链伸出手来“回头”时,你就违反了这个流程

有了这个警告,你就不会从你的恶棍那里得到回报。当链接then时,您还应该返回您的响应


对于这类问题,可能是一个更好的地方。

您好,您可以将回调函数作为参数传递给redux操作

export const createUser = (user, callBack) => {
    api.defaults.headers.common['Authorization'] = state.token.token;
    api.post("/users/create", user)
        .then(response => {
            //dispach(createUserSuccess(response.data));
            if (callBack !== undefined) {
                callBack(response.data, null);
            }
        }).catch(error => {
            error = errorManager(error);
            //dispach(createUserFailure(error));
            if (callBack !== undefined) {
                callBack(null, error);
            }
        });
}

您好,您可以将回调函数作为参数传递给redux操作

export const createUser = (user, callBack) => {
    api.defaults.headers.common['Authorization'] = state.token.token;
    api.post("/users/create", user)
        .then(response => {
            //dispach(createUserSuccess(response.data));
            if (callBack !== undefined) {
                callBack(response.data, null);
            }
        }).catch(error => {
            error = errorManager(error);
            //dispach(createUserFailure(error));
            if (callBack !== undefined) {
                callBack(null, error);
            }
        });
}