Redux API之compose
2016-03-15 17:13
495 查看
compose(...functions)
从右到左来组合多个函数。
这是函数式编程中的方法,为了方便,被放到了 Redux 里。 当需要把多个 store 增强器 依次执行的时候,需要用到它。
参数
- (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);View Code
小贴士
compose
做的只是让你不使用深度右括号的情况下来写深度嵌套的函数。不要觉得它很复杂。
相关文章推荐
- CentOS 6.5下利用Rsyslog+LogAnalyzer+MySQL部署日志服务器
- Java 从流中读取byte的奇怪现象,出现负值,详解
- IplImage 4通道转3通道
- python学习 3笔记
- 软件工程学习进度
- 几种简单排序的实现
- Android MVP 架构示例
- 169. Majority Element
- 如何用Scapy写一个端口扫描器?
- zabbix安装配置
- PAT (Top Level) Practise 1010 Lehmer Code (35)
- Arcgis server发布切片替换之前的注意事项
- Android ViewDragHelper完全解析 自定义ViewGroup神器
- 多重json转换为JAVA对象
- 三种内存问题与三种指针
- web进修之—Hibernate起步(1)
- 在某一数值范围内取随机数
- Leet code 67. Add Binary
- 【Oracle】 Oracle全文检索功能建立
- iOS UIView 一些属性方法总结