[Redux] Introduction
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的更多相关文章
- React笔记
React JS Tutorials for Beginners - 1 - Getting Started https://www.youtube.com/watch?v=-AbaV3nrw6E&a ...
- 为了弄懂Flutter的状态管理, 我用10种方法改造了counter app
为了弄懂Flutter的状态管理, 我用10种方法改造了counter app 本文通过改造flutter的counter app, 展示不同的状态管理方法的用法. 可以直接去demo地址看代码: h ...
- [转] 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 ...
- 前端小白第一次使用redux存取数据练习
在学习了redux基本教程后,课程参考如下网址:https://www.redux.org.cn/docs/introduction/CoreConcepts.html,开始着手练习 1.首先编写一个 ...
- Vuex、Flux、Redux、Redux-saga、Dva、MobX
https://www.jqhtml.com/23003.html 这篇文章试着聊明白这一堆看起来挺复杂的东西.在聊之前,大家要始终记得一句话:一切前端概念,都是纸老虎. 不管是Vue,还是 Reac ...
- React进阶篇(2) -- Redux
前言 如果还不知道为什么要使用Redux,说明你暂时还不需要它. 三大原则 单一数据源 整个应用的 state 被储存在一棵 object tree 中,并且这个 object tree 只存在于唯一 ...
- Redux & React & react-redux
Redux Redux & React & react-redux https://redux.js.org/ https://redux.js.org/api https://red ...
- 谁都能听懂的Redux+Redux-Saga超级傻瓜教程
对不起本文确实有标题党的嫌疑:) 想要理解本文还是要先会用react和es6,如果连react和es6都不知道是什么的话我也没辙:( 如果你选择用react来开发应用,并且你没对各个组件的状态进行应有 ...
- react/redux组件库、模板、学习教程
开源的有蚂蚁金服的: 1.https://pro.ant.design/index-cn 2.https://pro.ant.design/docs/getting-started-cn 3.http ...
随机推荐
- Jquery 操作 select
1.判断select选项中 是否存在Value="paraValue"的Item $("#selectid option[@value='paraValue']" ...
- IOS常用CGRect的交错,边缘,中心的检测
转自:http://tsyouaschen.iteye.com/blog/1946957 判断给定的点是否被一个CGRect包含,可以用CGRectContainsPoint函数 BOOL conta ...
- React 点击删除列表中对应项(React 获取DOM中自定义属性)
点击删除按钮,删除列表中对应项本来是React比较基础的应用,可是应用情况变得复杂了以后,我还真想了一会儿才搞定. 简化一下应用场景:点击新增按钮,增加一条输入框,点击输入框旁边的按钮,删除该输入框( ...
- Mysql软删除
所谓软删除(Soft Deleting),即在删除数据表中的数据时,并不直接将其从数据表中删除,而是将其标志为删除,即在每张表中设置一个删除字段(如:IsDeleted)等,默认情况下其值为0,及未删 ...
- HashMap、HashTable学习
HashMap: HashMap 继承于AbstractMap,实现了Map.Cloneable.java.io.Serializable接口. HashMap 的实现不是同步的,这意味着它不是线程安 ...
- C++中public、protected、private
C++中public.protected.private 第一:private, public, protected 访问标号的访问范围. private:只能由 1.该类中的函数. 2.其友元函数 ...
- mysql的limit经典用法及优化
用法一 SELECT `keyword_rank`.* FROM `keyword_rank` WHERE (advertiserid='59') LIMIT 2 OFFSET 1; 比如这个 ...
- 终于通过了PMP考试,然这只是一个开始。。。
三个月的辛苦付出,从2015/06/18(本人的生日)开始接受培训,2015/10/6终于收到了PMI发过来的祝贺的邮箱,但是成绩不是很理想.只得了两个B,三个M.但是目标已实现,心情回落. 在这三个 ...
- canvas 渐变
那么第一种渐变方式就是LinearGradient,具体实施就是以下代码: var colorStyle=context.createLinearGradient(0,0,0,HEIGHT); col ...
- [C]记录C语言中由于粗心遇到的奇葩错误.
1. 正确代码: for( word = strtok( buf, whitespace); word != NULL; word = strtok( NULL, whitespace)) { ) 错 ...