1. /**
  2. * A reducer for a single todo
  3. * @param state
  4. * @param action
  5. * @returns {*}
  6. */
  7. const todo = ( state, action ) => {
  8. switch ( action.type ) {
  9. case 'ADD_TODO':
  10. return {
  11. id: action.id,
  12. text: action.text,
  13. completed: false
  14. };
  15. case 'TOGGLE_TODO':
  16. if ( state.id !== action.id ) {
  17. return state;
  18. }
  19.  
  20. return {
  21. ...state,
  22. completed: !state.completed
  23. };
  24. default:
  25. return state;
  26. }
  27. };
  28.  
  29. /**
  30. * The reducer for the todos
  31. * @param state
  32. * @param action
  33. * @returns {*}
  34. */
  35. const todos = ( state = [], action ) => {
  36. switch ( action.type ) {
  37. case 'ADD_TODO':
  38. return [
  39. ...state,
  40. todo( undefined, action )
  41. ];
  42. case 'TOGGLE_TODO':
  43. return state.map( t => todo( t, action ) );
  44. default:
  45. return state;
  46.  
  47. }
  48. };
  49.  
  50. /**
  51. * Reducer for the visibilityFilter
  52. * @param state
  53. * @param action
  54. * @returns {*}
  55. */
  56. const visibilityFilter = ( state = 'SHOW_ALL',
  57. action ) => {
  58. switch ( action.type ) {
  59. case 'SET_VISIBILITY_FILTER':
  60. return action.filter;
  61. default:
  62. return state;
  63. }
  64. };
  65.  
  66. /**
  67. * combineReducers: used for merge reducers togethger
  68. * createStore: create a redux store
  69. */
  70. const { combineReducers, createStore } = Redux;
  71. const todoApp = combineReducers( {
  72. todos,
  73. visibilityFilter
  74. } );
  75.  
  76. const store = createStore( todoApp );
  77.  
  78. /**
  79. * For generate todo's id
  80. * @type {number}
  81. */
  82. let nextTodoId = 0;
  83.  
  84. /**
  85. * React related
  86. */
  87. const {Component} = React;
  88. class TodoApp extends Component {
  89. render() {
  90. return (
  91. <div>
  92. <input ref={
  93. (node)=>{
  94. this.input = node
  95. }
  96. }/>
  97. <button onClick={
  98. ()=>{
  99. //After clicking the button, dispatch a add todo action
  100. store.dispatch({
  101. type: 'ADD_TODO',
  102. id: nextTodoId++,
  103. text: this.input.value
  104. })
  105. this.input.value = "";
  106. }
  107. }>ADD todo
  108. </button>
  109. <ul>
  110. //loop thought the todo list
  111. {this.props.todos.map( ( todo )=> {
  112. return (
  113. <li key={todo.id}
  114. style={{
  115. textDecoration: todo.completed ?
  116. 'line-through' : 'none'
  117. }}
  118.  
  119. onClick={ ()=>{
  120. store.dispatch({
  121. type: 'TOGGLE_TODO',
  122. id: todo.id
  123. })
  124. }}
  125. >
  126. {todo.text}
  127. </li>
  128. )
  129. } )}
  130. </ul>
  131. </div>
  132. );
  133. }
  134. }
  135.  
  136. const render = () => {
  137. ReactDOM.render(
  138. <TodoApp todos={store.getState().todos}/>,
  139. document.getElementById( 'root' )
  140. );
  141. };
  142.  
  143. //Every time, store updated, also fire the render() function
  144. store.subscribe( render );
  145. render();

[Redux] React Todo List Example (Toggling a Todo)的更多相关文章

  1. [Redux] Writing a Todo List Reducer (Toggling a Todo)

    Learn how to implement toggling a todo in a todo list application reducer. let todo = (state = [], a ...

  2. [Redux] React Todo List Example (Filtering Todos)

    /** * A reducer for a single todo * @param state * @param action * @returns {*} */ const todo = ( st ...

  3. [Redux] Writing a Todo List Reducer (Adding a Todo)

    Learn how to implement adding a todo in a todo list application reducer. let todo = (state = [], act ...

  4. RxJS + Redux + React = Amazing!(译一)

    今天,我将Youtube上的<RxJS + Redux + React = Amazing!>翻译(+机译)了下来,以供国内的同学学习,英文听力好的同学可以直接看原版视频: https:/ ...

  5. RxJS + Redux + React = Amazing!(译二)

    今天,我将Youtube上的<RxJS + Redux + React = Amazing!>的后半部分翻译(+机译)了下来,以供国内的同学学习,英文听力好的同学可以直接看原版视频: ht ...

  6. Redux & React & react-redux

    Redux Redux & React & react-redux https://redux.js.org/ https://redux.js.org/api https://red ...

  7. Redux React & Online Video Tutorials

    Redux React & Online Video Tutorials https://scrimba.com/@xgqfrms https://scrimba.com/c/cEwvKNud ...

  8. [Redux] React Todo List Example (Adding a Todo)

    Learn how to create a React todo list application using the reducers we wrote before. /** * A reduce ...

  9. React开发入门:以开发Todo List为例

    目录 概述 React基本概念 JSX是什么? 设置React APP 初始化APP 应用结构 探索第一个React组件 index.js 变量和props JSX中的变量 组件props props ...

随机推荐

  1. MyEclipse Web Project导入Eclipse Dynamic Web Project,无法部署到tomcat问 题

    做作业遇到一个小问题,将MyEclipse Web Project导入到Eclipse中开发.在部署到tomcat时,发现无法发布这个项目. 问题分析: MyEclipse Web Project被识 ...

  2. UFLDL课程学习(一)

    章节地址:http://ufldl.stanford.edu/tutorial/supervised/LinearRegression/ 章节名称:线性回归 (Linear Regression) 第 ...

  3. 使用URL读取网络图片资源

    URL(Uniform Resource Locator) 对象代表统一资源定位器. 代码如下: public class MainActivity extends ActionBarActivity ...

  4. 前端/html5效果收藏

    H5应用 9款漂亮的H5效果 8款漂亮的H5效果 36漂亮的button效果 颜色RGB表 省市二级联动

  5. 用Spring Boot零配置快速创建web项目(1)

    一.Spring Boot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人 ...

  6. Ehcache入门(一)——开发环境的搭建

    EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. 那么.如何搭建Ehcache的开发环境呢? 1.下载相关的jar包,这 ...

  7. vim编辑器命令

    Vim介绍 vim(vimsual)是Linux/Unix系列OS中通用的全屏编辑器. vim分为两种状态,即命令状态和编辑状态,在命令状态下,所键入的字符系统均作为命令来处理,如:q代表退出,而编辑 ...

  8. linux 目录说明

    1./bin /usr/bin  /usr/local/bin   都是放置用户可执行二进制文件. 2./boot 主要是放置liunx系统启动时用到的文件. 2./dev   文件夹内主要是西东外设 ...

  9. 转:初学者,手工注入测试方法小节 (出处:: 51Testing软件测试网--jie)

    1.加入单引号 ’提交,  结果:如果出现错误提示,则该网站可能就存在注入漏洞.    2.数字型判断是否有注入; 语句:and 1=1 ;and 1=2 (经典).' and '1'=1(字符型)  ...

  10. 利用js得到某个范围内的整数随机数

    Math.random()方法可以返回(0,1)之间的随机数,不包括0和1. 套用公式:Math.floor(Math.random()*可能的值的总数+第一个可能的值) 如得到(3,9]之间的随机数 ...