0

我学习 React Redux 并在此处阅读有关使用mapDispatchToPropsbindActionCreators

bindActionCreators像这样单独使用它时效果很好:

export default connect(null, (dispatch) =>
  bindActionCreators(actionCreators, dispatch)
)(FormContainer);

但我有更多的行动并尝试了这个:

function mapDispatchToProps(dispatch) {
  return {
    bindActionCreators(actionCreators, dispatch),
    clearPosts: () => dispatch(clearPosts()),
  }
}

这会产生如下错误:

在此处输入图像描述

. 我如何将它们结合起来我阅读了文档,但我对此一无所知,请咨询

4

2 回答 2

2

如果您真的想使用connect.

const mapDispatchToProps = {
    ...actionCreators,
    clearPosts
}

但是,如果可以使用函数组件,建议使用 redux hooks 而不是 connect ,因为它们通常更容易使用。

(如果您正在关注向您展示的教程connect,它可能已过时,请参阅官方 redux 教程

于 2021-04-07T13:35:58.910 回答
1

试试下面的代码

 function mapDispatchToProps(dispatch) {
      return {
        todoActions: bindActionCreators(actionCreators, dispatch),
        clearPosts: () => dispatch(clearPosts()),
      }
    }
于 2021-04-07T11:10:19.857 回答