我需要 bindActionCreators 吗?
Do i need bindActionCreators?
我正在读这个 post,我注意到他没有使用 redux
附带的 bindActionCreators
方法,这是为什么?
我不需要吗?
这里是post:https://medium.com/@stowball/a-dummys-guide-to-redux-and-thunk-in-react-d8904a7005d3
如果你不应该使用它,为什么它存在?我很困惑。
我已经按照他在 post 中所说的做了:
function matchDispatchToProps(dispatch){
return {
fetchQp: (url) => dispatch(qpFetchData(url))
};
}
使用 bindActionCreators
进行此对比有何不同?
The only use case for bindActionCreators is when you want to pass some
action creators down to a component that isn't aware of Redux, and you
don't want to pass dispatch or the Redux store to it.
你提到的 post 怎么样 - 那家伙确实将他的组件连接到 Redux
- 所以他没有明确使用 bindActionCreators
。
这些例子都是等价的:
function mapDispatchToProps(dispatch) {
return {
fetchQp : (url) => dispatch(qpFetchData(url))
}
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({fetchQp : qpFetchData}, dispatch);
}
const mapDispatchToProps = {
fetchQp : qpFetchData
}
// in all three cases, used as:
export default connect(mapStateToProps, mapDispatchToProps)(MyComponent);
就我个人而言,我建议使用第三种形式("object shorthand")。只需将一个充满动作创建者的对象作为第二个参数传递给 connect
,Redux 将自动在内部使用 bindActionCreators
- 没有充分的理由自己编写一个单独的 mapDispatch
函数。
我正在读这个 post,我注意到他没有使用 redux
附带的 bindActionCreators
方法,这是为什么?
我不需要吗?
这里是post:https://medium.com/@stowball/a-dummys-guide-to-redux-and-thunk-in-react-d8904a7005d3
如果你不应该使用它,为什么它存在?我很困惑。
我已经按照他在 post 中所说的做了:
function matchDispatchToProps(dispatch){
return {
fetchQp: (url) => dispatch(qpFetchData(url))
};
}
使用 bindActionCreators
进行此对比有何不同?
The only use case for bindActionCreators is when you want to pass some action creators down to a component that isn't aware of Redux, and you don't want to pass dispatch or the Redux store to it.
你提到的 post 怎么样 - 那家伙确实将他的组件连接到 Redux
- 所以他没有明确使用 bindActionCreators
。
这些例子都是等价的:
function mapDispatchToProps(dispatch) {
return {
fetchQp : (url) => dispatch(qpFetchData(url))
}
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({fetchQp : qpFetchData}, dispatch);
}
const mapDispatchToProps = {
fetchQp : qpFetchData
}
// in all three cases, used as:
export default connect(mapStateToProps, mapDispatchToProps)(MyComponent);
就我个人而言,我建议使用第三种形式("object shorthand")。只需将一个充满动作创建者的对象作为第二个参数传递给 connect
,Redux 将自动在内部使用 bindActionCreators
- 没有充分的理由自己编写一个单独的 mapDispatch
函数。