【发布时间】:2019-07-28 19:01:36
【问题描述】:
我正在查看一个 React-Redux 应用程序并尝试了解一切是如何工作的。
在其中一个组件中,我看到了以下代码行:
import { bindActionCreators } from "redux";
...
function mapDispatchToProps(dispatch) {
return bindActionCreators({ fetchPhotos }, dispatch);
}
export default connect(
null,
mapDispatchToProps
)(SearchBar);
如果我将上面的代码更改为以下代码,一切仍然正常,没有任何错误:
function mapStateToProps(photos) {
return { photos };
}
export default connect(
mapStateToProps,
{ fetchPhotos }
)(SearchBar);
对我来说,似乎我使用 connect 的方式更容易理解,也不需要导入额外的库。
有什么理由要导入 bindActionCreators 并使用 mapDispatchToProps?
【问题讨论】:
标签: redux react-redux