理解redux中间件

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

原文地址:https://www.cnblogs.com/wust-hy/p/12590288.html

时间: 2024-08-28 03:02:24

理解redux中间件的相关文章

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

1. React有props和state: props意味着父级分发下来的属性[父组件的state传递给子组件  子组件使用props获取],state意味着组件内部可以自行管理的状态,并且整个React没有数据向上回溯的能力,也就是说数据只能单向向下分发,或者自行内部消化.理解这个是理解React和Redux的前提.2. 一般构建的React组件内部可能是一个完整的应用,它自己工作良好,你可以通过属性作为API控制它.但是更多的时候发现React根本无法让两个组件互相交流,使用对方的数据.然后

Redux:中间件

redux中间件概念 比较容易理解. 在使用redux时,改变store state的一个固定套路是调用store.dispatch(action)方法,将action送到reducer中. 所谓中间件,就是在dispatch发送action  和  action到达reducer  之间,加入一些中间层,对action进行处理. 在之前学习redux的异步操作时,用到的redux-thunk就是一个中间件.action抵达reducer之前先对其进行判断,如果是对象就直接送到reducer:如

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

今天我们来聊聊Redux,这篇文章是一个进阶的文章,建议大家先对redux的基础有一定的了解,在这里给大家推荐一下阮一峰老师的文章: http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_one_basic_usages.html 对于基础部分我在这里稍微讲解一下 首先我们要知道我们为什么要使用Redux,我们在什么情况下才需要去使用Redux,在这里引用Redux的创造者的一句话:"只有遇到 React 实在解决不了的问题,你才需

redux中间件

怎么自定义一个中间件呢? 根据 redux 文档,中间件的签名如下: ({ getState, dispatch }) => next => action 根据上文的 applyMiddleware 源码,每个中间件接收 getState & dispatch 作为参数,并返回一个函数,该函数会被传入下一个中间件的 dispatch 方法,并返回一个接收 action 的新函数. 以一个打印 dispatch action 前后的 state 为例,创建一个中间件示例: export

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

教程源代码及目录 https://github.com/lewis617/myReact 今天,我们要讲解的是自定义redux中间件这个知识点.本节内容非常抽象,特别是中间件的定义原理,那多层的函数嵌套和串联,需要极强逻辑思维能力才能完全消化吸收.不过我会多罗嗦几句,所以不用担心. 例子 例子是官方的例子real-world,做的是一个获取github用户.仓库的程序. 本例子源代码: https://github.com/lewis617/myReact/tree/master/redux-e

Redux中间件随笔

说到Redux中间件,我见到的他的应用,在一个高星项目中,是做数据的异步获取,后端接口调用的角色.我甚感此种方式的精明之处.在此写个文章,任诸君品鉴,若有不到之处,不吝赐教. 1.什么是中间件?既然叫了中间件,就有中间件的共性.Express中中间件,就是一个拦截的作用,每次的request,都会被所截获,然后做你想要做的目的.如果你想放行,就next();继续下个中间件.同理Redux中,request=>action,request变成了action.其他就是一样的.每个触发action都会

redux中间件和redux-thunk实现原理

redux-thunk这个中间件可以使我们把这样的异步请求或者说复杂的逻辑可以放到action里面去处理,redux-thunk使redux的一个中间件,为什么叫做中间件 我们说中间件,那么肯定是谁和谁的中间,那么redux的中间件指的是谁和谁的中间呢? 如图.view在redux中会派发一个action,action通过store的dispatch方法派发给store,store接收到action,连同之前到state,一起传给reducer,reducer返回一个新到数据给store,sto

Redux 中间件的执行顺序理解

Redux.applyMiddleware(thunk, middleware1) 和 Redux.applyMiddleware(middleware1, thunk) 的区别: <!DOCTYPE HTML> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>Redux<

浅谈redux 中间件的原理

在使用redux管理异步数据流的时候,我们会使用中间件,以redux-thunk中间件为例,我们做一下分析: 首先是构建store,我们需要以下代码进行揉入中间件的类似creatStore函数的构造: const loggerMiddleware = createLogger(); const createStoreWithMiddleware = applyMiddleware( thunkMiddleware, loggerMiddleware )(createStore); export