代码一:import { addTodo } from './actionCreators'import { bindActionCreators } from 'redux'function mapStateToProps(state) { return { todos: state.todos }
}function mapDispatchToProps(dispatch) { return bindActionCreators({ addTodo }, dispatch)
}export default connect(mapStateToProps, mapDispatchToProps)(TodoApp)代码二:import { addTodo } from './actionCreators'function mapStateToProps(state) { return { todos: state.todos }
}const mapDispatchToProps = {
addTodo
}export default connect(mapStateToProps, mapDispatchToProps)(TodoApp)以上两段代码的作用都是注入 todos 和特定的 action 创建函数,有一点疑惑的地方代码一中调用了bindActionCreators 而代码二中没有调用,两段代码出来的效果都是一样的。请问bindActionCreators 的作用是什么?
1 回答
慕虎7371278
TA贡献1802条经验 获得超4个赞
function bindActionCreator(actionCreator, dispatch) { return (...args) => dispatch(actionCreator(...args)); }/* * @param actionCreators * @param dispatch * @return {actionKey: (...args) => dispatch(actionCreator(...args))} */export default function bindActionCreators(actionCreators, dispatch) { return mapValues(actionCreators, actionCreator => bindActionCreator(actionCreator, dispatch) ); }
bindActionCreators部分源码,再里面调用了dispatch
方法
- 1 回答
- 0 关注
- 641 浏览
添加回答
举报
0/150
提交
取消