一、boundActionCreators
boundActionCreators接收两个参数:
1.actionCreators:可以是一个包含action创建函数的对象或者一个action函数,如果是一个action创建函数直接返回一个自动分发的action创建函数 2.dispatch:仓库的dispatch函数
二、源码:
function bindActionCreator(actionCreator
, dispatch
) {
return function() {
return dispatch(actionCreator
.apply(this, arguments
))
}
}
export default function bindActionCreators(actionCreators
, dispatch
) {
if (typeof actionCreators
=== 'function') {
return bindActionCreator(actionCreators
, dispatch
)
}
if (typeof actionCreators
!== 'object' || actionCreators
=== null) {
throw new Error(
`bindActionCreators expected an object or a function, instead received ${
actionCreators === null ? 'null' : typeof actionCreators
}. ` +
`Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?`
)
}
const boundActionCreators
= {}
for (const key
in actionCreators
) {
const actionCreator
= actionCreators
[key
]
if (typeof actionCreator
=== 'function') {
boundActionCreators
[key
] = bindActionCreator(actionCreator
, dispatch
)
}
}
return boundActionCreators
}