一.什么是redux-actions
redux-actions是一个简化action和reducer创建的一个封装库,里面有5个js文件,
createAction.js
handleAction.js
handleActions.js
index.js
ownKeys.js
二.怎么使用?下面将从源码一一解释每个文件的的用处
1.createAction.js
从名字就可以看出,这是用来创建action的.其源码如下:
1 /** 2 * 参数不是function调用此函数 3 */ 4 function identity(t) { 5 return t; 6 } 7 8 /** 9 * 创建action 10 * @param type action的类型 11 * @param actionCreator 需要创建的action,函数 12 * @param metaCreator action的属性 13 * @returns {Function} 14 */ 15 export default function createAction(type, actionCreator, metaCreator) { 16 /** 17 * finalActionCreator最终创建的action, 18 * 判断传进来的参数是不是function,true返回这个函数,false调用identity函数 19 */ 20 const finalActionCreator = typeof actionCreator === 'function' 21 ? actionCreator 22 : identity; 23 /** 24 * 返回一个匿名函数 25 */ 26 return (...args) => { 27 /** 28 *创建的action,只有两个属性 29 */ 30 const action = { 31 type, 32 payload: finalActionCreator(...args) 33 }; 34 /** 35 * 如果给匿名函数传递参数的长度为1个,或者第一个参数元素的类型为Error,那么这么action的error属性为true 36 */ 37 if (args.length === 1 && args[0] instanceof Error) { 38 // Handle FSA errors where the payload is an Error object. Set error. 39 action.error = true; 40 } 41 /** 42 * 传递到action里面的函数 43 */ 44 if (typeof metaCreator === 'function') { 45 action.meta = metaCreator(...args); 46 } 47 48 return action; 49 }; 50 }
2.handleAction.js
操作action,源码如下:
1 import { isError } from 'flux-standard-action'; 2 /** 3 * 判断是不是function 4 */ 5 function isFunction(val) { 6 return typeof val === 'function'; 7 } 8 /** 9 * 处理action 10 * @param type action类型 11 * @param 所有的reducers 12 * @returns {Function} 13 */ 14 export default function handleAction(type, reducers) { 15 return (state, action) => { 16 // If action type does not match, return previous state 17 if (action.type !== type) return state; 18 19 const handlerKey = isError(action) ? 'throw' : 'next'; 20 21 // If function is passed instead of map, use as reducer 22 if (isFunction(reducers)) { 23 reducers.next = reducers.throw = reducers; 24 } 25 26 // Otherwise, assume an action map was passed 27 const reducer = reducers[handlerKey]; 28 29 return isFunction(reducer) 30 ? reducer(state, action) 31 : state; 32 }; 33 }
reduce-reducers源码:
1 export default function reduceReducers(...reducers) { 2 return (previous, current) => 3 reducers.reduce( 4 (p, r) => r(p, current), 5 previous 6 ); 7 }
3.handleActions.js
将所有的action集中在一起处理
1 import handleAction from './handleAction'; 2 import ownKeys from './ownKeys'; 3 import reduceReducers from 'reduce-reducers'; 4 5 /** 6 * 7 * @param handlers 所有的action 8 * @param defaultState 初始的state 9 * @returns {Function} 返回reducer 10 */ 11 export default function handleActions(handlers, defaultState) { 12 const reducers = ownKeys(handlers).map(type => { 13 return handleAction(type, handlers[type]); 14 }); 15 /** 16 * 处理过后的reduce 17 */ 18 const reducer = reduceReducers(...reducers) 19 20 return typeof defaultState !== 'undefined' 21 ? (state = defaultState, action) => reducer(state, action) 22 : reducer; 23 }
4.ownKeys.js
用于判断对象属性的工具
1 export default function ownKeys(object) { 2 /** 3 * Reflect.ownKeys类似于Object.keys(),返回对象中可枚举的属性 4 */ 5 if (typeof Reflect !== 'undefined' && typeof Reflect.ownKeys === 'function') { 6 return Reflect.ownKeys(object); 7 } 8 /** 9 * 返回对象自己(非原型继承的属性)的属性名称,包括函数。 10 * Object.keys只适用于可枚举的属性,而Object.getOwnPropertyNames返回对象自己的全部属性名称。 11 */ 12 let keys = Object.getOwnPropertyNames(object); 13 /** 14 * getOwnPropertySymbols 15 * 返回Symbol类型的属性 16 */ 17 if (typeof Object.getOwnPropertySymbols === 'function') { 18 keys = keys.concat(Object.getOwnPropertySymbols(object)); 19 } 20 21 return keys; 22 }
5.index.js
输出所有的函数
1 import createAction from './createAction'; 2 import handleAction from './handleAction'; 3 import handleActions from './handleActions'; 4 5 export { 6 createAction, 7 handleAction, 8 handleActions 9 };