[Redux] React Todo List Example (Toggling a Todo)
- /**
- * A reducer for a single todo
- * @param state
- * @param action
- * @returns {*}
- */
- const todo = ( state, action ) => {
- switch ( action.type ) {
- case 'ADD_TODO':
- return {
- id: action.id,
- text: action.text,
- completed: false
- };
- case 'TOGGLE_TODO':
- if ( state.id !== action.id ) {
- return state;
- }
- return {
- ...state,
- completed: !state.completed
- };
- default:
- return state;
- }
- };
- /**
- * The reducer for the todos
- * @param state
- * @param action
- * @returns {*}
- */
- const todos = ( state = [], action ) => {
- switch ( action.type ) {
- case 'ADD_TODO':
- return [
- ...state,
- todo( undefined, action )
- ];
- case 'TOGGLE_TODO':
- return state.map( t => todo( t, action ) );
- default:
- return state;
- }
- };
- /**
- * Reducer for the visibilityFilter
- * @param state
- * @param action
- * @returns {*}
- */
- const visibilityFilter = ( state = 'SHOW_ALL',
- action ) => {
- switch ( action.type ) {
- case 'SET_VISIBILITY_FILTER':
- return action.filter;
- default:
- return state;
- }
- };
- /**
- * combineReducers: used for merge reducers togethger
- * createStore: create a redux store
- */
- const { combineReducers, createStore } = Redux;
- const todoApp = combineReducers( {
- todos,
- visibilityFilter
- } );
- const store = createStore( todoApp );
- /**
- * For generate todo's id
- * @type {number}
- */
- let nextTodoId = 0;
- /**
- * React related
- */
- const {Component} = React;
- class TodoApp extends Component {
- render() {
- return (
- <div>
- <input ref={
- (node)=>{
- this.input = node
- }
- }/>
- <button onClick={
- ()=>{
- //After clicking the button, dispatch a add todo action
- store.dispatch({
- type: 'ADD_TODO',
- id: nextTodoId++,
- text: this.input.value
- })
- this.input.value = "";
- }
- }>ADD todo
- </button>
- <ul>
- //loop thought the todo list
- {this.props.todos.map( ( todo )=> {
- return (
- <li key={todo.id}
- style={{
- textDecoration: todo.completed ?
- 'line-through' : 'none'
- }}
- onClick={ ()=>{
- store.dispatch({
- type: 'TOGGLE_TODO',
- id: todo.id
- })
- }}
- >
- {todo.text}
- </li>
- )
- } )}
- </ul>
- </div>
- );
- }
- }
- const render = () => {
- ReactDOM.render(
- <TodoApp todos={store.getState().todos}/>,
- document.getElementById( 'root' )
- );
- };
- //Every time, store updated, also fire the render() function
- store.subscribe( render );
- render();
[Redux] React Todo List Example (Toggling a Todo)的更多相关文章
- [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 ...
- [Redux] React Todo List Example (Filtering Todos)
/** * A reducer for a single todo * @param state * @param action * @returns {*} */ const todo = ( st ...
- [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 ...
- RxJS + Redux + React = Amazing!(译一)
今天,我将Youtube上的<RxJS + Redux + React = Amazing!>翻译(+机译)了下来,以供国内的同学学习,英文听力好的同学可以直接看原版视频: https:/ ...
- RxJS + Redux + React = Amazing!(译二)
今天,我将Youtube上的<RxJS + Redux + React = Amazing!>的后半部分翻译(+机译)了下来,以供国内的同学学习,英文听力好的同学可以直接看原版视频: ht ...
- Redux & React & react-redux
Redux Redux & React & react-redux https://redux.js.org/ https://redux.js.org/api https://red ...
- Redux React & Online Video Tutorials
Redux React & Online Video Tutorials https://scrimba.com/@xgqfrms https://scrimba.com/c/cEwvKNud ...
- [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 ...
- React开发入门:以开发Todo List为例
目录 概述 React基本概念 JSX是什么? 设置React APP 初始化APP 应用结构 探索第一个React组件 index.js 变量和props JSX中的变量 组件props props ...
随机推荐
- MyEclipse Web Project导入Eclipse Dynamic Web Project,无法部署到tomcat问 题
做作业遇到一个小问题,将MyEclipse Web Project导入到Eclipse中开发.在部署到tomcat时,发现无法发布这个项目. 问题分析: MyEclipse Web Project被识 ...
- UFLDL课程学习(一)
章节地址:http://ufldl.stanford.edu/tutorial/supervised/LinearRegression/ 章节名称:线性回归 (Linear Regression) 第 ...
- 使用URL读取网络图片资源
URL(Uniform Resource Locator) 对象代表统一资源定位器. 代码如下: public class MainActivity extends ActionBarActivity ...
- 前端/html5效果收藏
H5应用 9款漂亮的H5效果 8款漂亮的H5效果 36漂亮的button效果 颜色RGB表 省市二级联动
- 用Spring Boot零配置快速创建web项目(1)
一.Spring Boot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人 ...
- Ehcache入门(一)——开发环境的搭建
EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. 那么.如何搭建Ehcache的开发环境呢? 1.下载相关的jar包,这 ...
- vim编辑器命令
Vim介绍 vim(vimsual)是Linux/Unix系列OS中通用的全屏编辑器. vim分为两种状态,即命令状态和编辑状态,在命令状态下,所键入的字符系统均作为命令来处理,如:q代表退出,而编辑 ...
- linux 目录说明
1./bin /usr/bin /usr/local/bin 都是放置用户可执行二进制文件. 2./boot 主要是放置liunx系统启动时用到的文件. 2./dev 文件夹内主要是西东外设 ...
- 转:初学者,手工注入测试方法小节 (出处:: 51Testing软件测试网--jie)
1.加入单引号 ’提交, 结果:如果出现错误提示,则该网站可能就存在注入漏洞. 2.数字型判断是否有注入; 语句:and 1=1 ;and 1=2 (经典).' and '1'=1(字符型) ...
- 利用js得到某个范围内的整数随机数
Math.random()方法可以返回(0,1)之间的随机数,不包括0和1. 套用公式:Math.floor(Math.random()*可能的值的总数+第一个可能的值) 如得到(3,9]之间的随机数 ...