[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 = [], action) => {

  switch(action.type){
    case ‘ADD_ITEM‘:
      return state = [
        ...state,
        {
          text: action.text,
          id: action.id,
          completed: false
        }
      ];
    default:
      return state;
  }
};

let testTodo = () => {
  let stateBefore = [];
  let action = {
    type: ‘ADD_ITEM‘,
    text: ‘Learn Redux‘,
    id: 0
  };
  let stateAfter = [
    {
      text: ‘Learn Redux‘,
      id: 0,
      completed: false,
    }
  ];

  deepFreeze(stateBefore);
  deepFreeze(action);

  expect(
    todo(stateBefore, action)
  ).toEqual(stateAfter);
};

testTodo();

console.log("All tests passed!");
时间: 2024-10-07 05:30:10

[Redux] Writing a Todo List Reducer (Adding a Todo)的相关文章

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

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': ret

[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 ( s

[Redux] Reducer Composition with Arrays

In the previous lesson we created a reducer that can handle two actions, adding a new to-do, and toggling an existing to-do. Right now, the code to update the to-do item or to create a new one is placed right inside of the to-dos reducer. This functi

redux+flux(一:入门篇)

React是facebook推出的js框架,React 本身只涉及UI层,如果搭建大型应用,必须搭配一个前端框架.也就是说,你至少要学两样东西,才能基本满足需要:React + 前端框架. Facebook官方使用的是 Flux 框架.本文就介绍如何在 React 的基础上,使用 Flux 组织代码和安排内部逻辑. 首先,Flux将一个应用分成四个部分: Flux 的最大特点,就是数据的"单向流动". 用户访问 View View 发出用户的 Action Dispatcher 收到

[Redux] Normalizing the State Shape

We will learn how to normalize the state shape to ensure data consistency that is important in real-world applications. We currently represent the todos in the state free as an array of todo object. However, in the real app, we probably have more tha

[Redux] Colocating Selectors with Reducers

We will learn how to encapsulate the knowledge about the state shape in the reducer files, so that the components don’t have to rely on it. In current VisibleTodoList.js: import { connect } from 'react-redux'; import { withRouter } from 'react-router

Redux系列x:源码解析

写在前面 redux的源码很简洁,除了applyMiddleware比较绕难以理解外,大部分还是 这里假设读者对redux有一定了解,就不科普redux的概念和API啥的啦,这部分建议直接看官方文档. 此外,源码解析的中文批注版已上传至github,可点击查看.本文相关示例代码,可点击查看. 源码解析概览 将redux下载下来,然后看下他的目录结构. npm install redux 这里我们需要关心的主要是src目录,源码解析需要关心的文件都在这里面了 index.js:redux主文件,主

手动实现redux(二)

一个app中store(状态树)是唯一的 我们知道对于一个app, store应该是唯一,集中用reducer管理的,那么当app中有多个页面,每个页面有多个组件时,就应该有多个reducer来管理. 当我某个组件dispatch出去一个action(动作)时,store接收到action,应该交给谁来管理呢?这里我们暂时无法使store根据不同的action来交由相应的reducer处理. combineReducer combineReducer函数将多个不同的reducer整合在一起,然后

Redux 的基础概念-API

三个基本原则 整个应用只有唯一一个可信数据源,也就是只有一个 Store State 只能通过触发 Action 来更改 State 的更改必须写成纯函数,也就是每次更改总是返回一个新的 State,在 Redux 里这种函数称为 Reducer Actions Action 很简单,就是一个单纯的包含 { type, payload } 的对象,type 是一个常量用来标示动作类型,payload 是这个动作携带的数据.Action 需要通过 store.dispatch() 方法来发送. 比