[Redux] Extracting Action Creators
2016-02-12 03:32
429 查看
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.
Then update the dispatch function:
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)) } } }
相关文章推荐
- apache commons io 2.2(六)工具部分
- 使用Cookie并使用MD5算法实现用户永久登录
- PLC多种编程方法例举
- 无符号数相减问题 C语言
- Android开发常用的36个代码片段
- [Redux] Generating Containers with connect() from React Redux (FooterLink)
- 程序员必须注意的十大编程禁忌
- HDU 1016 Prime Ring Problem(DFS)
- Linux 学习树之基础20160212
- 提升Python编程效率的10点建议
- 小白自学敲代码的零散知识点整理
- [Redux] Generating Containers with connect() from React Redux (AddTodo)
- tkinter界面卡死的解决办法
- day16-----------集合框架
- 【CodeForces 618B】Guess the Permutation
- Asymptotic notation
- TCP/IP协议模型详解
- 【CodeForces 624C】Graph and String
- apache commons io 2.2(五)Output部分
- HDU 1241 Oil Deposits(DFS模板题)