redux questions :

1. reducers 函数如何创建和聚合
2. action创建函数如何如何包裹在dispatch函数中
3. 如何给默认的dispatch方法增加中间件能力

middleware:

(js context)中间件由函数组合的形式创建,实现与主要执行任务正交的功能。
tips:类似的有装饰器模式
在 redux middleware 要装饰的主要功能是 dispatch 函数。dispatch 函数的主要功能是发送actions 给
reducer函数来产生新的state。
applyMiddleware.js
1. 函数组合
    f(x) = 2x 
    g(x) = x^2+3x+1
    (f * g)(x) = f(g(x)) = f(2x) = 4x^2 + 6x + 1
    组合两个或者更多的函数,返回一个新的函数
    function compose(f,g){
        return x=>f(g(x))
    }
    从里向外求值
2. 柯里化(curring)
    通过柯里化,可以创建一个拥有部分信息的新函数
    (bind some information)
    function curring(a){
        return b=>a+b
    }
    通过函数组合和柯里化能够为数据处理创建一个管道
redux midddleware 被设计用来在daispatch调用之前创建组合在一起的函数。
对dispatch的过程做包装,不改变任何值。
 1 export default function applyMiddleware(...middlewares) {
2 return createStore => (...args) => {
3 const store = createStore(...args)
4 let dispatch = () => {
5 throw new Error(
6 'Dispatching while constructing your middleware is not allowed. ' +
7 'Other middleware would not be applied to this dispatch.'
8 )
9 }
10
11 const middlewareAPI = {
12 getState: store.getState,
13 dispatch: (...args) => dispatch(...args)
14 }
15 const chain = middlewares.map(middleware => middleware(middlewareAPI))
16 dispatch = compose(...chain)(store.dispatch)
17 // dispatch is the new
18 return {
19 ...store,
20 dispatch
21 }
22 }
23 }
24
25 //dispatch in createStore
26 // dispatch action and run the listener callBack
27 function dispatch(action) {
28 try {
29 isDispatching = true
30 currentState = currentReducer(currentState, action)
31 } finally {
32 isDispatching = false
33 }
34
35 const listeners = (currentListeners = nextListeners)
36 for (let i = 0; i < listeners.length; i++) {
37 const listener = listeners[i]
38 listener()
39 }
40
41 return action
42 }
43
44
45 // In createStore.js, the enhance is the function returned by applyMiddleware
46 // return a store which dispatch is decorated
47 enhance(createStore)(reducer,preloadState)
48
49 // example looger middleware
50 function logger({getState})=>{
51 return (next)=>(action)=>{
52 // action
53 const returnNext = next(action)
54 const state = getState()
55 console.log(`${ation.type}=>${state}`)
56 // doSomething
57 return returnNext
58 }
59 }

参考:

https://medium.com/@meagle/understanding-87566abcfb7a

理解redux中间件的更多相关文章

  1. 理解 Redux 中间件机制

    Redux 的 action 是一个 JS 对象,它表明了如何对 store 进行修改.但是 Redux 的中间件机制使action creator 不光可以返回 action 对象,也可以返回 ac ...

  2. 3.3 理解 Redux 中间件(转)

    这一小节会讲解 redux 中间件的原理,为下一节讲解 redux 异步 action 做铺垫,主要内容为: Redux 中间件是什么 使用 Redux 中间件 logger 中间件结构分析 appl ...

  3. redux深入理解之中间件(middleware)

    理解reduce函数 reduce() 方法接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始缩减,最终为一个值. arr.reduce([callback, initi ...

  4. 理解 Redux 的中间件

    将该思想抽象出来,其实和 Redux 就无关了.问题变成,怎样实现在截获函数的执行,以在其执行前后添加自己的逻辑. 为了演示,我们准备如下的示例代码来模拟 Redux dispatch action ...

  5. 理解Redux以及如何在项目中的使用

    今天我们来聊聊Redux,这篇文章是一个进阶的文章,建议大家先对redux的基础有一定的了解,在这里给大家推荐一下阮一峰老师的文章: http://www.ruanyifeng.com/blog/20 ...

  6. 通俗易懂的理解 Redux(知乎)

    1. React有props和state: props意味着父级分发下来的属性[父组件的state传递给子组件  子组件使用props获取],state意味着组件内部可以自行管理的状态,并且整个Rea ...

  7. Redux:中间件

    redux中间件概念 比较容易理解. 在使用redux时,改变store state的一个固定套路是调用store.dispatch(action)方法,将action送到reducer中. 所谓中间 ...

  8. react+redux教程(七)自定义redux中间件

    今天,我们要讲解的是自定义redux中间件这个知识点.本节内容非常抽象,特别是中间件的定义原理,那多层的函数嵌套和串联,需要极强逻辑思维能力才能完全消化吸收.不过我会多罗嗦几句,所以不用担心. 例子 ...

  9. 轻松理解Redux原理及工作流程

    轻松理解Redux原理及工作流程 Redux由Dan Abramov在2015年创建的科技术语.是受2014年Facebook的Flux架构以及函数式编程语言Elm启发.很快,Redux因其简单易学体 ...

  10. 【React全家桶入门之十三】Redux中间件与异步action

    在上一篇中我们了解到,更新Redux中状态的流程是这种:action -> reducer -> new state. 文中也讲到.action是一个普通的javascript对象.red ...

随机推荐

  1. nginx微信对接

    location /MP_verify_l47ZUDtvieeDlVWR.txt { default_type text/html; return 200 'l47ZUDtvieeDlVWR'; }

  2. rust 网上资料记录(自用)

    最近要学嵌入式的rust,记录一些资料的url,方便自己查阅 书籍 常用的: rust圣经(不是权威指南那本)https://course.rs/ 中文 rust 参考手册 https://rustw ...

  3. LeetCode_单周赛_329

    2544. 交替数字和 代码1 转成字符串,逐个判断 class Solution { public int alternateDigitSum(int n) { char[] s = (" ...

  4. Elemen ui&表单 、CRUD、安装

    ElementUI表单 Form表单,每一个表单域是由一个form-item组件构成的,表单域中可以放置各种类型的表单控键,有input.switch.checkbox 表单的绑定form 内容分别是 ...

  5. echarts使用dataset数据集创建单轴散点图

    dataset创建单轴散点图 由于使用echarts作图时,我很喜欢用dataset作为数据源,但是官方案例中,有没有给出相关示例,于是,在翻阅官方文档相关案例之后,结合官方文档使用dataset的示 ...

  6. Solon v2.2.1 发布。向 Graalvm Native 友好靠近

    本次更新最重要的是增加了 Solon APT 项目,为更简单的完成 Graalvm Native 打包提供了帮助:其次是增加了 @ProxyComponent 和 @SolonMain 注解:以及优化 ...

  7. mybatis懒加载

    mybatis懒加载全局配置 <settings> <setting name="mapUnderscoreToCamelCase" value="tr ...

  8. 第八周作业-N67044-张铭扬

    1. 完成ftp的mysql虚拟用户 数据库服务器:10.0.0.152 FTP服务器:10.0.0.156 #配置数据库服务器 [root@centos8 ~]# yum -y install ma ...

  9. Kotlin相关语法

    1.Kotlin的匿名函数 { val  a = 1 val b = 2 a+b } 就是一个不带名字的函数体 2.Kotlin的函数类型 函数类型:用来声明一个函数参数和返回值形式的 特殊数据类型声 ...

  10. 任务队列神器:Celery 入门到进阶指南

    任务队列神器:Celery 入门到进阶指南 发布于2021-03-24 16:24:53阅读 1.9K0   1.什么是celery celery是一个简单,灵活.可靠的分布式任务执行框架,可以支持大 ...