dispatch和bindActionCreators有什么区别?

问题描述:

如果我们连接到行动通过调度有两种方式: -dispatch和bindActionCreators有什么区别?

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

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

    }); 

如果我们做同样与bindActionCreators的帮助,那么我们 代码,我们将: -

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

现在我的问题是,哪一个我应该使用发货或 bindActionCreators?他们有什么区别?

这实际上是同样的事情。的

bindActionCreators({ editLabResult: requestEmployees}, dispatch); 

结果是你手动创建的内容:

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

据了Redux bindActionCreators文档:

打开一个对象,其值是行动的创造者,与 相同的按键对象,但与每个动作创造者包装成一个调度 调用,以便他们可以直接调用。

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

而不是使用bindActionCreators的,你可以将对象传递给connect方法,它会做包装你:

connect(mapStateToProps, { editLabResult: requestEmployees, anotherAction, etc... })