小编典典

dispatch和bindActionCreators之间有什么区别?

reactjs

如果我们使用分派连接到动作,则有两种方法:

1. this.props.dispatch(requestEmployees());

2. const mapDispatchToProps = (dispatch) => ({
      requestEmployees: () => dispatch(requestEmployees())

    });

如果我们在bindActionCreators的帮助下做同样的事情,那么我们的代码将是:

function matchDispatchToProps(dispatch) {
        return bindActionCreators({ editLabResult: requestEmployees}, dispatch);
    }

现在我的问题是,应该使用 调度还是 bindActionCreators中的 哪一个?它们之间有什么区别?


阅读 264

收藏
2020-07-22

共1个答案

小编典典

实际上是同一回事。的结果

bindActionCreators({ editLabResult: requestEmployees}, dispatch);

是您手动创建的:

requestEmployees: () => dispatch(requestEmployees())

根据redux
bindActionCreators文档:

将值是动作创建者的对象转换为具有相同键的对象,但每个动作创建者都包装在 调度 调用中,以便可以直接调用它们。

bindActionCreators({ editLabResult: requestEmployees, anotherAction, etc... }, dispatch);

bindActionCreators您可以将对象传递给connect方法,而不是使用,它将为您包装:

connect(mapStateToProps, { editLabResult: requestEmployees, anotherAction, etc... })
2020-07-22