redux里面的概念不少,有Action、ActionCreator、reducer、store、middleware、Provider、Connect……概念不理解,就是眉毛胡子一把抓,弄不出头绪。redux的概念,经过一张图你们就一目了然了。git
这张图大体能够归纳redux的整个流程。从图中咱们能够看出,Action是数据流动的开始,Reducer负责业务逻辑处理,store是整个流程的中轴。github
一、你从哪里来?——谈谈actions的起源ajax
redux里的action和flux里的action大致上一致,不一样的是设计理念的差异。flux里的action是以函数的形式,但在redux里,action被设计成普通的js对象。编程
{
type: 'ADD_TODO',
text: 'Build my first Redux app'
}
这就是一个最基本的action,必须注意的是,type是action必须的一个key值,是action的惟一标识。其它的key可使任意像传递的数据结构。
可是actions到底表明什么?
actions表明着数据来源的萃取。咱们的view界面须要不少数据结构,如ajax返回的数据、路由状态跟踪、UI状态等。。。关键的是这些数据都是动态流动的,既然流动,就要有入口、有方向。actions就是表明数据流动的开始,携带的key值中,type表明发生了什么事情,其它的就是须要流动的数据结构。
概括起来,actions是数据从应用传到store的有效载荷,是store惟一的数据来源。
那么如何产生一个action,这就是又有一个概念actionCreator:redux
function addTodo(text) {数据结构
return { type: ADD_TODO, text };
}
actionCreator其实是一个返回action值的函数而已。这样,只需把 action 生成器的结果传给 dispatch() 方法便可实例化 dispatch。app
dispatch(addTodo(text));
二、条条大路通哪里?——看看reducer的神奇async
action表示发生了什么事情,代表了有事情发生,确定会对应用的数据产生影响。咱们知道,React组件内的数据都是以state的形式存在的,state表明着数据结构。那么就要问了,action的发生到底会对state产生什么影响。这就是reducer的做用了。
reducer 实际上是一个函数, 接收旧的 state 和 action, 返回新的 state。ide
(previousState, action) => newState
这种设计来源于函数式编程思想,简单,易懂,没有反作用。函数式编程
function todoApp(state = initialState, action) {
switch (action.type) {
case SET_VISIBILITY_FILTER:
return Object.assign({}, state, { visibilityFilter: action.filter });
default:
return state;
}
}
能够看出,业务逻辑部分都是在reducer里处理。这就涉及一个问题,一个应用里有不少逻辑部分,都放在一个reducer里面会形成很是的臃肿。实际过程当中,把业务进行分拆,而后经过combineReducer函数合并成一个reducer。详细能够看这里:
https://github.com/rackt/redux/blob/mast...
切记,在reducer不要改变旧的state值。
三、看看你是谁?——揭开store的面纱
首先咱们看文档里怎么描述的:
The Store is the object that brings themtogether.(them指的是action和reducer)
It’s important to note that you’ll only have a single store in a Redux application.
这就说明,Store负责把reducer和action结合的做用。store怎么建立?通常是经过下面的代码:
const store = createStore(reducer);
这个createStore又是什么函数,咱们看看createStore.js源码:
import isPlainObject from './utils/isPlainObject'; export var ActionTypes = { INIT: '@@redux/INIT' }; export default function createStore(reducer, initialState) { if (typeof reducer !== 'function') { throw new Error('Expected the reducer to be a function.'); } var currentReducer = reducer; var currentState = initialState; var listeners = []; var isDispatching = false; function getState() { return currentState; } function subscribe(listener) { listeners.push(listener); return function unsubscribe() { var index = listeners.indexOf(listener); listeners.splice(index, 1); }; } function dispatch(action) { if (!isPlainObject(action)) { throw new Error( 'Actions must be plain objects. ' + 'Use custom middleware for async actions.' ); } if (typeof action.type === 'undefined') { throw new Error( 'Actions may not have an undefined "type" property. ' + 'Have you misspelled a constant?' ); } if (isDispatching) { throw new Error('Reducers may not dispatch actions.'); } try { isDispatching = true; currentState = currentReducer(currentState, action); } finally { isDispatching = false; } listeners.slice().forEach(listener => listener()); return action; } function replaceReducer(nextReducer) { currentReducer = nextReducer; dispatch({ type: ActionTypes.INIT }); } dispatch({ type: ActionTypes.INIT }); return { dispatch, subscribe, getState, replaceReducer }; }
咱们能够看到createStore返回的是一个对象,该对象有四种方法,分别是:
dispatch, subscribe, getState, replaceReducer
能够看出redux里的store有点像flux里的dispatcher的做用,产生action能够经过store.dispatch发送出去,想监听状态能够经过store.subscribe订阅。
值得注意的是,程序初始化的时候,redux会发送一个类型为@@redux/INIT的action,来初始化state。
以上就是我对redux流程前半部分的理解,请批评指正。