博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Redux] React Todo List Example (Adding a Todo)
阅读量:4332 次
发布时间:2019-06-06

本文共 2737 字,大约阅读时间需要 9 分钟。

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

 

/** * 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 (            
{ this.input = node } }/>
    //loop thought the todo list {
    this.props.todos.map( ( todo )=> { return
  • {todo.text}
  • } )}
); }}const render = () => { ReactDOM.render(
, document.getElementById( 'root' ) );};//Every time, store updated, also fire the render() functionstore.subscribe( render );render();

 

         JS Bin  

 

转载于:https://www.cnblogs.com/Answer1215/p/5125218.html

你可能感兴趣的文章
单工、半双工和全双工的定义
查看>>
Hdu【线段树】基础题.cpp
查看>>
时钟系统
查看>>
BiTree
查看>>
5个基于HTML5的加载动画推荐
查看>>
水平权限漏洞的修复方案
查看>>
静态链接与动态链接的区别
查看>>
Android 关于悬浮窗权限的问题
查看>>
如何使用mysql
查看>>
linux下wc命令详解
查看>>
敏捷开发中软件测试团队的职责和产出是什么?
查看>>
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>
阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合
查看>>
阶段3 2.Spring_01.Spring框架简介_04.spring发展历程
查看>>