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 做的只是让你不使用深度右括号的情况下来写深度嵌套的函数。不要觉得它很复杂。

最新文章

  1. [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
  2. 谈一谈IOC、DI
  3. cocos2d-x打飞机实例总结(一):程序入口分析和AppDelegate,Application,ApplicationProtocol三个类的分析
  4. virtualenv 安装 lxml
  5. 如何下载android官网Lib包
  6. 用GCD线程组与GCD信号量将异步线程转换为同步线程
  7. 廖雪峰js教程笔记 2
  8. Linux操作系统奥秘02-系统引导(GRUB)
  9. Uploading Files in SharePoint 2013 using CSOM and REST
  10. angularJS中ng-if的用法
  11. JDBC的批量查询报告内存溢出解决方法
  12. Gtk中的文本视图(GtkTexViewWidget)
  13. 【转载】C++应用引用计数技术
  14. c#中传递参数前加out
  15. 文字分列 CSS属性
  16. 题解 P3246 【[HNOI2016]序列】
  17. 【原创】XAF 常见错误以及对应解决方法
  18. OpenStack 安装:neutron服务
  19. 什么是CSS hack?
  20. APP支付-》支付宝RSA2->支付与验签

热门文章

  1. python读取文件存到excel中
  2. 【LeetCode】【数组归并】Merge k Sorted Lists
  3. 20145229吴姗珊 《Java程序设计》课程总结
  4. poj 3126 Prime Path 【bfs】
  5. poj 1032 Parliament 【思维题】
  6. BI入门经典(转载)
  7. 十七 Django框架,文件上传
  8. BEC listen and translation exercise 47
  9. 关于_T()说明
  10. codeforces 637A A. Voting for Photos(水题)