compose(...functions)

从右到左来组合多个函数。

这是函数式编程中的方法,为了方便,被放到了 Redux 里。 当需要把多个 store 增强器 依次执行的时候,需要用到它。

参数

  1. (arguments): 需要合成的多个函数。每个函数都接收一个函数作为参数,然后返回一个函数。

返回值

(Function): 从右到左把接收到的函数合成后的最终函数。

示例

下面示例演示了如何使用 compose 增强 store,这个 store 与 applyMiddleware 和 redux-devtools 一起使用。

 import { createStore, combineReducers, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import * as reducers from '../reducers/index'; let reducer = combineReducers(reducers);
let middleware = [thunk]; let finalCreateStore; // 生产环境中,我们希望只使用 middleware。
// 而在开发环境中,我们还希望使用一些 redux-devtools 提供的一些 store 增强器。
// UglifyJS 会在构建过程中把一些不会执行的死代码去除掉。 if (process.env.NODE_ENV === 'production') {
finalCreateStore = applyMiddleware(...middleware)(createStore);
} else {
finalCreateStore = compose(
applyMiddleware(...middleware),
require('redux-devtools').devTools(),
require('redux-devtools').persistState(
window.location.href.match(/[?&]debug_session=([^&]+)\b/)
),
createStore
); // 不使用 compose 来写是这样子:
//
// finalCreateStore =
// applyMiddleware(middleware)(
// devTools()(
// persistState(window.location.href.match(/[?&]debug_session=([^&]+)\b/))(
// createStore
// )
// )
// );
} let store = finalCreateStore(reducer);

小贴士

  • compose 做的只是让你不使用深度右括号的情况下来写深度嵌套的函数。不要觉得它很复杂。

Redux API之compose的更多相关文章

  1. Redux API

    Redux API ​ Redux的API非常少.Redux定义了一系列的约定(contract),同时提供少量辅助函数来把这些约定整合到一起. ​ Redux只关心如何管理state.在实际的项目中 ...

  2. React深入 - 手写redux api

    简介: 手写实现redux基础api createStore( )和store相关方法 api回顾: createStore(reducer, [preloadedState], enhancer) ...

  3. redux中的compose源码分析

    1. redux中compose用来组合各种中间件来实现链式调用,例子如下 compose( applyMiddleware, devTools, persistState, createStore ...

  4. Redux API之applyMiddleware

    applyMiddleware(...middlewares) 使用包含自定义功能的 middleware 来扩展 Redux 是一种推荐的方式.Middleware 可以让你包装 store 的di ...

  5. Redux API之Store

    Store Store 就是用来维持应用所有的 state 树 的一个对象. 改变 store 内 state 的惟一途径是对它 dispatch 一个action. Store 不是类.它只是有几个 ...

  6. Redux API之bindActionCreators

    bindActionCreators(actionCreators,dispatch) 把 action creators 转成拥有同名 keys 的对象,但使用 dispatch 把每个 actio ...

  7. Redux API之combineReducers

    combineReducers(reducers) 随着应用变得复杂,需要对 reducer 函数 进行拆分,拆分后的每一块独立负责管理 state 的一部分. combineReducers 辅助函 ...

  8. Redux API之creatStore

    createStore(reducer, [initialState]) 创建一个 Redux store 来以存放应用中所有的 state.应用中应有且仅有一个 store. 参数 reducer  ...

  9. Redux基础

    Redux 是一个状态容器 Redux 就像是作者自己的介绍,它不会为你提供任何的东西,它不会告诉你如何做路由,它只专注于应用程序状态,是一个 JavasSript 的状态容器,所有的状态的变化都是当 ...

随机推荐

  1. 【leetcode刷提笔记】Permutations

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  2. 0417 封装 property、classmethod、staricmethod

    一.封装 把一堆东西装在一个容器里 函数和属性装到了一个非全局的命名空间 class A: __N = 123 # 静态变量 def func(self): print(A.__N) # 在类的内部使 ...

  3. 基于msm8909高通平台Android驱动开发之hello程序

    本文转载自:http://www.itwendao.com/article/detail/227839.html Android驱动开发之Hello实例:   驱动部分 modified:   ker ...

  4. 【Java】-BigInteger大数类的使用【超强Java大数模板 总结】

    Scanner cin = new Scanner(new BufferedInputStream(System.in)); 这样定义Scanner类的对象读入数据可能会快一些! 参考这个博客继续补充 ...

  5. ELK常用API使用方法

    以下ELK系列文章参考自http://www.tianyiqingci.com/ 总目录: Monitor API ElasticSearch聚合分析API Elasticsearch信息检索API ...

  6. Jboss添加Windows服务,同时定期重启

    一.添加成Windows服务 进入目录 \wildfly-9.0.2s - All\bin\service\ 编辑 service.bat,修改一下参数 set SHORTNAME=SAMEXAppS ...

  7. Hadoop- HDFS的API操作

    1.引入依赖 <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop- ...

  8. el表达式判断字符串相等

    el表达式判断字符串相等 Java code 1 ${"a" == "a"}  ${"b" eq "b"}  都可以 & ...

  9. SpringMVC注解示例

    1.web.xml <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-cla ...

  10. 分享知识-快乐自己:solr 伪集群搭建

    前言:在你搭建集群之前先去搭建一个单机版的 跳转 Solr 集群构建图: 1):部署(4个)Tomcat 下载 更改每一个 tomcat 启动端口号: [root@VMSolr tomcat_clus ...