[Redux] Using mapDispatchToProps() Shorthand Notation

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Redux] Using mapDispatchToProps() Shorthand Notation相关的知识,希望对你有一定的参考价值。

We will learn how to avoid the boilerplate code in mapDispatchToProps() for the common case where action creator arguments match the callback prop arguments.

 

Current code:

// visibleTodoList.js
const mapDispatchToProps = (dispatch) => {
return {
onTodoClick: (id) => {
dispatch(toggleTodo(id));
},
};
};

const VisibleTodoList = withRouter(connect(
mapStateToProps,
mapDispatchToProps
)(TodoList));

 

The id we pass from onTodoClick is the same as in toggleTodo, then we can use the shorthard notation to get rid of mapDispatchToProps function:

const VisibleTodoList = withRouter(connect(
    mapStateToProps,
    {onTodoClick: toggleTodo}
)(TodoList));

 

以上是关于[Redux] Using mapDispatchToProps() Shorthand Notation的主要内容,如果未能解决你的问题,请参考以下文章

react jsflux implementation using redux 给出错误:意外字符'?

[Redux] Using withRouter() to Inject the Params into Connected Components

[Functional Programming ADT] Initialize Redux Application State Using The State ADT

[Recompose] Add Local State with Redux-like Reducers using Recompose

es6 中的装饰器是如何工作的?

TypeError: _this.store.getState is not a function when using connect from Redux