Single immutable state tree:

  Should be just one single javascript object.

Describing the changes by action:

  every change in the application state as a plain JavaScript object called “action”.

Pure Function & Impure Function:

Pure function always return the same result and no side effect.

Impure function, has side effect or return new function.

// Pure functions
function square(x) {
return x * x;
}
function squareAll(items) {
return items.map(square);
} // Impure functions
function square(x) {
updateXInDatabase(x);
return x * x;
}
function squareAll(items) {
for (let i = 0; i < items.length; i++) {
items[i] = square(items[i]);
}
}

The Reducer function:

  The function should have a 'previous state', 'the action has been dispatched', '¨next state' and this function should be pure.

Writing a Counter Reducer with Tests

function counter(state, action) {

  if(typeof state === "undefined"){
return 0;
} if(action.type === "INCREASE"){
state += 1;
return state;
}else if(action.type === "DECREASE"){
state -= 1;
return state;
}else{
return state;
}
} expect(
counter(0, {type: 'INCREASE'})
).toEqual(1); expect(
counter(1, {type: 'INCREASE'})
).toEqual(2); expect(
counter(2, {type: 'DECREASE'})
).toEqual(1); expect(
counter(1, {type: 'DECREASE'})
).toEqual(0); // If the action is unknown, should remain be the previsou state
expect(
counter(1, {type: 'SOMETHING_ELSE'})
).toEqual(1); // If the previous state is undefined, should return the initialize state
expect(
counter(undefined, {})
).toEqual(0); console.log("PASSED!");

Covert to ES6:

const counter = (state = 0, action) => {

  switch(action.type) {
case "INCREASE:
return state + 1;
case "DECREASE":
return state -1;
default:
return state;
}
}

[Redux] Introduction的更多相关文章

  1. React笔记

    React JS Tutorials for Beginners - 1 - Getting Started https://www.youtube.com/watch?v=-AbaV3nrw6E&a ...

  2. 为了弄懂Flutter的状态管理, 我用10种方法改造了counter app

    为了弄懂Flutter的状态管理, 我用10种方法改造了counter app 本文通过改造flutter的counter app, 展示不同的状态管理方法的用法. 可以直接去demo地址看代码: h ...

  3. [转] What is the point of redux when using react?

    As I am sure you have heard a bunch of times, by now, React is the V in MVC. I think you can think o ...

  4. 前端小白第一次使用redux存取数据练习

    在学习了redux基本教程后,课程参考如下网址:https://www.redux.org.cn/docs/introduction/CoreConcepts.html,开始着手练习 1.首先编写一个 ...

  5. Vuex、Flux、Redux、Redux-saga、Dva、MobX

    https://www.jqhtml.com/23003.html 这篇文章试着聊明白这一堆看起来挺复杂的东西.在聊之前,大家要始终记得一句话:一切前端概念,都是纸老虎. 不管是Vue,还是 Reac ...

  6. React进阶篇(2) -- Redux

    前言 如果还不知道为什么要使用Redux,说明你暂时还不需要它. 三大原则 单一数据源 整个应用的 state 被储存在一棵 object tree 中,并且这个 object tree 只存在于唯一 ...

  7. Redux & React & react-redux

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

  8. 谁都能听懂的Redux+Redux-Saga超级傻瓜教程

    对不起本文确实有标题党的嫌疑:) 想要理解本文还是要先会用react和es6,如果连react和es6都不知道是什么的话我也没辙:( 如果你选择用react来开发应用,并且你没对各个组件的状态进行应有 ...

  9. react/redux组件库、模板、学习教程

    开源的有蚂蚁金服的: 1.https://pro.ant.design/index-cn 2.https://pro.ant.design/docs/getting-started-cn 3.http ...

随机推荐

  1. ORACLE导入导出操作篇

    1. DIRECTORY 指定转储文件和日志文件所在的目录DIRECTORY=directory_objectDirectory_object用于指定目录对象名称.需要注意,目录对象是使用CREATE ...

  2. Android开发手记(24) Log的使用及颜色的更改

    在程序开发过程中,LOG是广泛使用的用来记录程序执行过程的机制,它既可以用于程序调试,也可以用于产品运营中的事件记录.在Android系统中,提供了简单.便利的LOG机制,开发人员可以方便地使用.本文 ...

  3. CoreLocation+MapKit系统定位(含坐标以及详细地址)

    iOS8 之后出现一些新的配置 [self.manager requestWhenInUseAuthorization]; 并且在info.plist文件中增加 NSLocationWhenInUse ...

  4. 玩转iOS 9的UIDynamics(转)

    转自 http://www.cocoachina.com/ios/20150716/12613.html 本文由CocoaChina翻译小组成员AGSpider(微博)翻译自fancypixel的博客 ...

  5. UICollectionView出现the behavior of the UICollectionViewFlowLayout is not defined because:

    2015-01-28 21:55:17.790 Demo[636:9351] the behavior of the UICollectionViewFlowLayout is notdefined ...

  6. 新修改了EMA的计算方法,合并线性回归率的计算。和通达信的结果一模一样

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  7. jquery 的attr()方法解析

    我想用jquery的attr()方法修改一个li小圆点的背景颜色和外边框的时候:刚开始 $("#shanghai-btn").attr({background:"#999 ...

  8. webservice取文件修改时间,返回1601/1/1 8:00:00

    若文件查找不到,则会返回1601/1/1 8:00:00,若能正确查找到该文件,则返回正确的修改时间.

  9. 如何用angularjs制作一个完整的表格之一__创建简单表格

    初步接手人生的第一个项目,需要用angularjs制作表格和实现各种功能,因此遇到了各种问题和以前不熟悉的知识点,在此记录下来,以供大家学习交流,解决方式可能并不完善或符合规范,如果大家有更好的方式欢 ...

  10. HTML5 input控件 placeholder属性

    placeholder 属性提供可描述输入字段预期值的提示信息(hint),该提示会在输入字段为空时显示,并会在字段获得焦点时消失. <input placeholder="请输入姓名 ...