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. MySQL 选择数据库

    MySQL 选择数据库 在你连接到 MySQL 数据库后,可能有多个可以操作的数据库,所以你需要选择你要操作的数据库. 从命令提示窗口中选择MySQL数据库 在 mysql> 提示窗口中可以很简 ...

  2. 30.SSH配置文件模板.md

    [toc] 1.struts2 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts ...

  3. sql编程 1

    declare emp_count number;begin select count(*) into emp_count from emp where HIOREDATE >= TO_DATE ...

  4. html5--canvas学习笔记

    1. 添加<canvas>元素 right: <canvas id="myCanvas" width="300" height="3 ...

  5. java学用代码

    /** *Java获取IP代码 */ import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.ev ...

  6. Testlink接口使用方法-python语言远程调用

    deepin@deepin-pc:~/test$ cat libclienttestlink.py #!/usr/bin/env python3 # -*- coding: utf-8 -*- #! ...

  7. Python自动化运维之19、Paramiko模块和堡垒机实战

    paramiko模块 paramiko是一个用于做远程控制的模块,使用该模块可以对远程服务器进行命令或文件操作,值得一说的是,fabric和ansible内部的远程管理就是使用的paramiko来现实 ...

  8. 使用C#的自定义事件

    public class CarDealer { public Action<string> NewCarInfo; //使用系统定义的泛型委托 public void NewCarCom ...

  9. [转]C# FileSystemWatcher监控指定文件或目录的文件的创建、删除、改动、重命名等活动

    觉得这个很常用..比如一些软件.   http://www.rabbit8.cn/DoNet/407.html   FileSystemWatcher控件主要功能: 监控指定文件或目录的文件的创建.删 ...

  10. BROCADE 300和MD3200扩展柜FC SAN,截图

    这表示俺玩过,其实,这个光交换机在只有一个共享存储的情况下,可用可不用. FC BROCADE只是为了方便后期扩展. FC SAN之类的识别不靠IP,因为不是IP SAN嘛.但也是自己的识别体系.