We will create an anction creator to manage the dispatch actions, to keep code maintainable and self-documenting by extracting action creators from the components.

let nextTodoId = 0;
const ACTION_CREATOR = {
addTodo: (text) => {
return {
type: 'ADD_TODO',
id: nextTodoId++,
text
}
},
setVisibilityFilter: (filter) => {
return {
type: 'SET_VISIBILITY_FILTER',
filter
}
},
toggleTodo: (id) => {
return {
type: 'TOGGLE_TODO',
id
}
}
}

Then update the dispatch function:

...

let AddTodo = ({ dispatch }) => {
let input; return (
<div>
<input ref={node => {
input = node;
}} />
<button onClick={() => {
dispatch(ACTION_CREATOR.addTodo(input.value))
input.value = '';
}}>
Add Todo
</button>
</div>
);
};
AddTodo = connect()(AddTodo); ...
const mapDispatchToTodoListProps = (dispatch) => {
return {
onTodoClick: (id) => {
dispatch(ACTION_CREATOR.toggleTodo(id));
}
};
}; ....
const mapDispatchToLinkProps = (
dispatch,
ownProps
) => {
return {
onClick: () => {
dispatch(ACTION_CREATOR.setVisibilityFilter(ownProps.filter))
}
}
}

[Redux] Extracting Action Creators的更多相关文章

  1. [Redux] Avoid action type naming conflicts

    In redux, the action type is just a normal string type, it is easy to get naming conflicts in large ...

  2. [Redux] Extracting Container Components -- Complete

    Clean TodoApp Component, it doesn't need to receive any props from the top level component: const To ...

  3. [Redux] Redux: Extracting Container Components -- AddTodo

    Code to be refactored: const AddTodo = ({ onAddClick }) => { let input; return ( <div> < ...

  4. [Redux] Extracting Container Components -- VisibleTodoList

    Code to be refacted: const TodoList = ({ todos, onTodoClick }) => ( <ul> {todos.map(todo =& ...

  5. [Redux] Extracting Container Components (FilterLink)

    Learn how to avoid the boilerplate of passing the props down the intermediate components by introduc ...

  6. [Redux] Extracting Presentational Components -- TodoApp

    Finally, I just noticed that the to-do app component doesn't actually have to be a class. I can turn ...

  7. [Redux] Extracting Presentational Components -- Footer, FilterLink

    Code to be refactored: let nextTodoId = 0; class TodoApp extends Component { render() { const { todo ...

  8. [Redux] Extracting Presentational Components -- AddTodo

    The code to be refactored: let nextTodoId = 0; class TodoApp extends Component { render() { const { ...

  9. [Redux] Extracting Presentational Components -- Todo, TodoList

    Code to be refactored: let nextTodoId = 0; class TodoApp extends Component { render() { const { todo ...

随机推荐

  1. [Cycle.js] Main function and effects functions

    We need to give structure to our application with logic and effects. This lessons shows how we can o ...

  2. UICollectionView的基本使用

    这个控件,看起来与UITableView有点像,而且基本的用法也很相像哦!!! 我们来看看API: #pragma mark - UICollectionViewDataSource // 指定Sec ...

  3. C#基础:C#4.0权威指南 杂笔一

    1.c#中数组初始化的几种不同用法     int[] name = new int[NUM];       int[] name = {1, 2, 3, 4, 5, 6};       int[] ...

  4. CSS基础知识笔记(三)

    继承 继承是一种规则,它允许样式不仅应用于某个特定html标签元素,而且应用于其后代.比如下面代码:如某种颜色应用于p标签,这个颜色设置不仅应用p标签,还应用于p标签中的所有子元素文本,这里子元素为s ...

  5. c#读写cookie

    读 response.SetCokie(new HttpCookie("Color",TextBox1.Text);写 request.Cookies["color&qu ...

  6. 《第一行代码》学习笔记2-Android开发特色

    1.四大组件:活动(Activity),服务(Service),广播接收器(Broadcast Receiver),内容提供器(Content Provider). Activity:应用中看得到的东 ...

  7. OpenGL ES 2.0 纹理映射

    纹理坐标用符点数表示,范围一般从0.0到1.0,在纹理坐标系中.纹理坐标系原点在左上侧,向右为S轴,向下为T轴.两个轴的取值范围都是0.0-1.0. 纹理映射 纹理映射:把一幅纹理图应用到相应的几何图 ...

  8. [Leetcode] implement strStr() (C++)

    Github leetcode 我的解题仓库   https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...

  9. jQuery 文字截取

    HTML <div class="summary">    <p class="news">业内知道,当赵科林“过档”联想之初,恰逢联想 ...

  10. 手把手教你如何使用webpack+react

    上一篇随笔讲述了新手入门入门前端 里面提到的第四阶段跟上当前前端的发展需要入门一个框架和自动化工具,当时推荐的是webpack+react 今天正好有空,也把自己入门webpack + react 的 ...