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的 sysdate、trunc函数

    SQL> select trunc(sysdate)+1/24+3 from dual; TRUNC(SYSDATE)+1/24-------------------2015-08-14 01: ...

  2. Smarty环境配置

    Smaty优点:1.代码分离 2.缓存技术 使用步骤: 1.下载Smaty模板 2.将模板中那个lib文件夹复制到项目中(一般为根目录,并且重命名在此命名为Smarty), 3.配置PHP 1.新建一 ...

  3. http请求的cookie

    Cookie的作用: Cookie是用于维持服务端会话状态的,通常由服务端写入,在后续请求中,供服务端读取. HTTP请求,Cookie的使用过程 1.server通过HTTP Response中的& ...

  4. HTTP 状态响应码 意思详解/大全

    HTTP 状态响应码 意思详解/大全 转:http://blog.csdn.net/helin916012530/article/details/29842595 HTTP状态码(HTTP Statu ...

  5. CentOS 7 之Helloworld with c

    其实我也不知道是为了啥, 到了现在这种年纪还想学习Linux下的C语言编程.因为我一直就傻傻地认为机会是垂青有准备的人,也一直呆呆地认为活到老学到老.现在Android这么火,各种终端如雨后春笋,而这 ...

  6. 7 Reverse Integer(数字反转Easy)

    题目意思:int数字反转 考虑:越界问题 class Solution { public: int reverse(int x) { ; while(x){ ans=ans*+x%; x=x/; } ...

  7. WebService cxf 接口中获得拦截器参数

    1. 拦截器中put属性 Message message = PhaseInterceptorChain.getCurrentMessage(); message.put("AuthCode ...

  8. PHP应用程序的安全性

    无论在开发中,还是在面试时或者技术讨论时,安全性都是需要深入了解及掌握的. 目标 本教程目标是使您了解应该如何保护自己构建的 Web 应用程序.讲解如何防御最常见的安全威胁:SQL 注入.操纵 GET ...

  9. DeDe内容调用img

    文章内容页调用缩略图方法如下两种.第一种没有大小设置.原图显示.第二种.可以设大小, {dede:field.image/} <img src="{dede:field.litpic ...

  10. 你不知道的JavaScript(作用域和闭包)

    作用域和闭包 ・作用域 引擎:从头到尾负责整个JavaScript的编译及执行过程. 编译器:负责语法分析及代码生成等. 作用域:负责收集并维护由所有声明的标识符(变量)组成的一系列查询,并实施一套非 ...