function bindActionCreator(actionCreator, dispatch) {
return (...args) => {
dispatch(actionCreator.apply(this, args)) // 同步的方法
}
}
function bindActionCreators(actionCreators, dispatch) {
if(typeof actionCreators === 'function') {
return bindActionCreator(actionCreators, dispatch)
}
if(Object.prototype.toString.call(actionCreators) !== "[object Object]") {
throw new Error('actionCreators 必须是对象格式')
}
const bondActionCreators = {}
const actionKeys = Object.keys(actionCreators)
// 如果是对象就迭代绑定这个对象
for(const key of actionKeys) {
const value = actionCreators[key]
bondActionCreators[key] = bindActionCreator(value, dispatch)
}
return bondActionCreators
}
export default bindActionCreators