整個工作流程:
三大原則
1.單一資料來源(Store) 整個應用的State被存放在一棵Object tree中,並且這個Object tree只存在唯一一個Store中;
2.State是唯讀的 唯一改變 State 的方法就是觸發 Action,Action 是一個用於描述已發生事件的普通物件。 確保了所有的修改都能被集中化處理。
3.通過純函數Reducer來修改Store, Reducer 只是一些純函數,它接收先前的 State 和 Action,並返回新的 State。 即reducer(state, action) => new state
export default function createStore(reducer, preloadedState, enhancer) {
if (typeof preloadedState === 'function' && typeof enhancer === 'undefined') {
// 第二個引數是一個函數,沒有第三個引數的情況
enhancer = preloadedState;
preloadedState = undefined;
}
// 如果第三個引數是函數走下面的邏輯,返回一個新的createStore
if (typeof enhancer !== 'undefined') {
if (typeof enhancer !== 'function') {
// enhancer 不是函數就報錯
throw new Error('Expected the enhancer to be a function.');
}
// enhancer就是高階函數,強化了本身這個createStore的函數,拿到增強後的createStore函數去處理
// applyMiddleware這個函數還會涉及到這個
return enhancer(createStore)(reducer, preloadedState);
}
if (typeof reducer !== 'function') {
// reducer不是函數報錯
throw new Error('Expected the reducer to be a function.');
}
// 其他程式碼省略
return {
dispatch,
subscribe,
getState,
replaceReducer,
[$$observable]: observable,
};
}
export default function applyMiddleware(...middlewares) {
return (createStore) =>
(...args) => {
const store = createStore(...args);
let dispatch = () => {
throw new Error(
'Dispatching while constructing your middleware is not allowed. ' +
'Other middleware would not be applied to this dispatch.',
);
};
const middlewareAPI = {
getState: store.getState,
dispatch: (...args) => dispatch(...args),
};
const chain = middlewares.map((middleware) => middleware(middlewareAPI));
dispatch = compose(...chain)(store.dispatch);
return {
...store,
dispatch,
};
};
}
// 其實就是修改了dispatch
let store = applyMiddleware(middleware1,middleware2)(createStore)(rootReducer);
從執行結果看,這時候 state 已經變成了一個以這些 reducer 為 key 的物件;reducer 也變成了一個合併的 reducer;
遍歷執行所有的 reducer 的時候把 action 傳進去,返回新的 state;
export default function combineReducers(reducers) {
const reducerKeys = Object.keys(reducers);
const finalReducers = {};
for (let i = 0; i < reducerKeys.length; i++) {
const key = reducerKeys[i];
if (typeof reducers[key] === 'function') {
finalReducers[key] = reducers[key];
}
}
const finalReducerKeys = Object.keys(finalReducers);
/* 返回一個整合後的reducers */
return function combination(state = {}, action) {
let hasChanged = false;
const nextState = {};
for (let i = 0; i < finalReducerKeys.length; i++) {
const key = finalReducerKeys[i];
const reducer = finalReducers[key];
const previousStateForKey = state[key];
const nextStateForKey = reducer(previousStateForKey, action);
if (typeof nextStateForKey === 'undefined') {
const errorMessage = getUndefinedStateErrorMessage(key, action);
throw new Error(errorMessage);
}
nextState[key] = nextStateForKey;
hasChanged = hasChanged || nextStateForKey !== previousStateForKey;
}
return hasChanged ? nextState : state;
};
}
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;
}
var listeners = (currentListeners = nextListeners);
for (var i = 0; i < listeners.length; i++) {
var listener = listeners[i];
listener();
}
return action;
}
function logger(store) {
return function (next) {
return function (action) { // 新的 dispatch 函數
console.group(action.type);
console.info('dispatching', action);
let result = next(action);
console.log('next state', store.getState());
console.groupEnd();
return result;
};
};
}
import React from 'react';
import { createStore, applyMiddleware } from 'redux';
function createLogger({ getState, dispatch }) {
return (next) => (action) => {
const prevState = getState();
console.log('createLogger1');
const returnValue = next(action);
const nextState = getState();
const actionType = String(action.type);
const message = `action ${actionType}`;
console.log(`%c prev state`, `color: #9E9E9E`, prevState);
console.log(`%c action`, `color: #03A9F4`, action);
console.log(`%c next state`, `color: #4CAF50`, nextState);
return returnValue;
};
}
function createLogger2({ getState }) {
return (next) => (action) => {
const console = window.console;
const prevState = getState();
console.log('createLogger2');
const returnValue = next(action);
const nextState = getState();
const actionType = String(action.type);
const message = `action ${actionType}`;
console.log(`%c prev state2`, `color: #9E9E9E`, prevState);
console.log(`%c action2`, `color: #03A9F4`, action);
console.log(`%c next state2`, `color: #4CAF50`, nextState);
return returnValue;
};
}
const reducer = function (state = { number: 0 }, action) {
switch (action.type) {
case 'add':
return {
number: state.number + action.number,
};
default:
return state;
}
};
const store = createStore(
reducer,
applyMiddleware(createLogger, createLogger2),
);
store.subscribe(function () {
console.log(1111);
});
const { dispatch } = store;
const App = () => {
const handler = () => {
dispatch({ type: 'add', number: 10 });
};
return (
<div>
<button onClick={handler}>觸發redux</button>
</div>
);
};
export default App;
store 的屬性如下:
Redux 的資料流是這樣的:
介面 => action => reducer => store => react => virtual dom => 介面
將action物件轉為一個帶dispatch的方法
比如connect接收的mapDispatchToProps 是物件,會使用 bindActionCreators 處理; 接收 actionCreator 和 dispatch,返回一個函數;
function bindActionCreator(actionCreator, dispatch) {
// 返回一個函數
return function() {
return dispatch(actionCreator.apply(this, arguments))
}
}
function bindActionCreators(actionCreators, dispatch) {
if (typeof actionCreators === 'function') {
return bindActionCreator(actionCreators, dispatch)
}
const boundActionCreators = {}
for (const key in actionCreators) {
const actionCreator = actionCreators[key]
if (typeof actionCreator === 'function') {
boundActionCreators[key] = bindActionCreator(actionCreator, dispatch)
}
}
return boundActionCreators
}
const mapDispatchToProps = { // actionCreators 這是個集合,
onClick: (filter) => {
type: 'SET_VISIBILITY_FILTER',
filter: filter
};
}
轉換為:
const mapDispatchToProps = { // actionCreators 這是個集合,
onClick:function(filter) {
return dispatch({ // dispatch 是閉包中的方法
type: 'SET_VISIBILITY_FILTER',
filter: filter
})
}
}
函數套函數,compose(...chain)(store.dispatch)結果返回一個加強了的 dispatch;
這點和koa比較相似,這個 dispatch 在執行的時候會呼叫中介軟體。
function compose(...funcs) {
if (funcs.length === 0) {
return (arg) => arg;
}
if (funcs.length === 1) {
return funcs[0];
}
// 每一次reduce迭代都會返回一個加強版的dispatch
return funcs.reduce(
(a, b) =>
(...args) =>
a(b(...args)),
);
}
加強版 dispatch(一個方法,接收 action 引數),在中介軟體中用 next 表示,執行 next 之後,會形成一個鏈條。
// 以createStore為引數
(createStore) =>
(...args) => {};
樣板程式碼過多 增加一個 action 往往需要同時定義相應的 actionType 然後再寫相關的 reducer。例如當新增一個非同步載入事件時,需要同時定義載入中、載入失敗以及載入完成三個 actionType,需要一個相對應的 reducer 通過 switch 分支來處理對應的 actionType,冗餘程式碼過多;
目前已經存在著非常多的解決方案,比如dva redux-tookit等。
更新效率問題:由於使用不可變資料模式,每次更新 state 都需要拷貝一份完整的 state 造成了記憶體的浪費以及效能的損耗。
其實 redux 以及 react-redux 中內部已做優化,開發的時候使用 shouldComponentUpdate 等優化方法也可以應用,也可以用不可變資料結構如 immutable、Immr 等來解決拷貝開銷問題。