Redux API之compose
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);
小贴士
compose做的只是让你不使用深度右括号的情况下来写深度嵌套的函数。不要觉得它很复杂。
Redux API之compose的更多相关文章
- Redux API
Redux API Redux的API非常少.Redux定义了一系列的约定(contract),同时提供少量辅助函数来把这些约定整合到一起. Redux只关心如何管理state.在实际的项目中 ...
- React深入 - 手写redux api
简介: 手写实现redux基础api createStore( )和store相关方法 api回顾: createStore(reducer, [preloadedState], enhancer) ...
- redux中的compose源码分析
1. redux中compose用来组合各种中间件来实现链式调用,例子如下 compose( applyMiddleware, devTools, persistState, createStore ...
- Redux API之applyMiddleware
applyMiddleware(...middlewares) 使用包含自定义功能的 middleware 来扩展 Redux 是一种推荐的方式.Middleware 可以让你包装 store 的di ...
- Redux API之Store
Store Store 就是用来维持应用所有的 state 树 的一个对象. 改变 store 内 state 的惟一途径是对它 dispatch 一个action. Store 不是类.它只是有几个 ...
- Redux API之bindActionCreators
bindActionCreators(actionCreators,dispatch) 把 action creators 转成拥有同名 keys 的对象,但使用 dispatch 把每个 actio ...
- Redux API之combineReducers
combineReducers(reducers) 随着应用变得复杂,需要对 reducer 函数 进行拆分,拆分后的每一块独立负责管理 state 的一部分. combineReducers 辅助函 ...
- Redux API之creatStore
createStore(reducer, [initialState]) 创建一个 Redux store 来以存放应用中所有的 state.应用中应有且仅有一个 store. 参数 reducer ...
- Redux基础
Redux 是一个状态容器 Redux 就像是作者自己的介绍,它不会为你提供任何的东西,它不会告诉你如何做路由,它只专注于应用程序状态,是一个 JavasSript 的状态容器,所有的状态的变化都是当 ...
随机推荐
- HTML 获取屏幕,浏览器,页面的高度
1,物理尺寸和分辨率 容器的尺寸是指当前分辨率下的高度.宽度,而不是物理高度.宽度. 如:一个22寸的显示器,屏幕分辨率为1366 * 768,那么获取到的屏幕高度为1366px,宽度为768px. ...
- PAT 天梯赛 L2-003. 月饼 【贪心】
题目链接 https://www.patest.cn/contests/gplt/L2-003 思路 用贪心思路 最后注意一下 总售价有可能是浮点数 AC代码 #include <cstdio& ...
- Data Structure Array: Move all zeroes to end of array
http://www.geeksforgeeks.org/move-zeroes-end-array/ #include <iostream> #include <vector> ...
- LISP
LISP是一种通用高级计算机程序语言,长期以来垄断人工智能领域的应用.LISP作为因应人工智能而设计的语言,是第一个声明式系内函数式程序设计语言,有别于命令式系内过程式的C.Fortran和面向对象的 ...
- 【leetcode刷题笔记】Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 第三章 python中的字符串
一.字符串的基本操作 所有标准的序列操作对字符串同样适用,如索引.分片.乘法.判断成员是否存在.求长度.最大值和最小值等.记住一点,字符串是不可变的. 二.字符串中重要的方法 1.find(subst ...
- Python 3 软件开发规范
Python 3 软件开发规范 参考链接 http://www.cnblogs.com/linhaifeng/articles/6379069.html#_label14 对每个目录,文件介绍. #= ...
- 一看就懂的数据库范式介绍(1NF,2NF,3NF,BC NF,4NF,5NF)
原文:http://josh-persistence.iteye.com/blog/2200644 一.基本介绍 设计关系数据库时,遵从不同的规范要求,设计出合理的关系型数据库,这些不同的规范要求被称 ...
- shell文件相关指令
文件解压缩tar 请参考文档:http://blog.csdn.net/eroswang/article/details/5555415/ tar -zcvf ${standardpath}${fil ...
- 剑指offer之 调整奇数偶数数组位置
package Problem14; /* * 问题描述: * 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位与数组的前半部分,所有偶数位与数组的 * 后半部分 */ publ ...