React-Redux:动作必须是普通对象.使用自定义中间件进行异步操作 [英] React-Redux: Actions must be plain objects. Use custom middleware for async actions

查看:29
本文介绍了React-Redux:动作必须是普通对象.使用自定义中间件进行异步操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

未处理的拒绝(错误):操作必须是普通对象.将自定义中间件用于异步操作.

Unhandled Rejection (Error): Actions must be plain objects. Use custom middleware for async actions.

我想为每个帖子添加评论.因此,当运行获取帖子时,我想为所有帖子调用获取评论 API.

I wanted to add comments with every posts. So when fetch posts are run I want to call fetch comment API for all post.

export function bindComments(postId) {
  return API.fetchComments(postId).then(comments => {
    return {
      type: BIND_COMMENTS,
      comments,
      postId
    }
  })
}

推荐答案

必须在异步请求结束后调度.

You have to dispatch after the async request ends.

这行得通:

export function bindComments(postId) {
    return function(dispatch) {
        return API.fetchComments(postId).then(comments => {
            // dispatch
            dispatch({
                type: BIND_COMMENTS,
                comments,
                postId
            });
        });
    };
}

这篇关于React-Redux:动作必须是普通对象.使用自定义中间件进行异步操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆