2016-12-27 69 views
14

如果我們連接到行動通過調度有兩種方式: -dispatch和bindActionCreators有什麼區別?

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

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

    }); 

如果我們做同樣與bindActionCreators的幫助,那麼我們 代碼,我們將: -

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

現在我的問題是,哪一個我應該使用發貨或 bindActionCreators?他們有什麼區別?

回答

20

這實際上是同樣的事情。的

bindActionCreators({ editLabResult: requestEmployees}, dispatch); 

結果是你手動創建的內容:

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

據了Redux bindActionCreators文檔:

打開一個對象,其值是行動的創造者,與 相同的按鍵對象,但與每個動作創造者包裝成一個調度 調用,以便他們可以直接調用。

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

而不是使用bindActionCreators的,你可以將對象傳遞給connect方法,它會做包裝你:

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