2. \u4ece\u96f6\u5f00\u59cb\u5b9e\u73b0\u4e00\u4e2a Redux<\/strong><\/div>\n\u6211\u4eec\u5148\u5fd8\u8bb0 Redux \u7684\u6982\u5ff5\uff0c\u4ece\u4e00\u4e2a\u4f8b\u5b50\u5165\u624b\uff0c\u4f7f\u7528 create-react-app \u521b\u5efa\u4e00\u4e2a\u9879\u76ee: toredux\u3002<\/p>\n
\u4ee3\u7801\u76ee\u5f55: myredux\/to-redux \u4e2d\u3002<\/p>\n
\u5c06 public\/index.html \u4e2d body \u4fee\u6539\u4e3a\u5982\u4e0b:<\/p>\n
<\/p>\n
\u6211\u4eec\u8981\u5b9e\u73b0\u7684\u529f\u80fd\u5982\u4e0a\u56fe\u6240\u793a\uff0c\u5728\u70b9\u51fb\u6309\u94ae\u65f6\uff0c\u80fd\u591f\u4fee\u6539\u6574\u4e2a\u5e94\u7528\u7684\u5b57\u4f53\u7684\u989c\u8272\u3002<\/p>\n
\u4fee\u6539 src\/index.js \u5982\u4e0b(\u4ee3\u7801: to-redux\/src\/index1.js):<\/p>\n
let state = { \r\n color: 'blue' \r\n} \r\n\/\/\u6e32\u67d3\u5e94\u7528 \r\nfunction renderApp() { \r\n renderHeader(); \r\n renderContent(); \r\n} \r\n\/\/\u6e32\u67d3 title \u90e8\u5206 \r\nfunction renderHeader() { \r\n const header = document.getElementById('header'); \r\n header.style.color = state.color; \r\n} \r\n\/\/\u6e32\u67d3\u5185\u5bb9\u90e8\u5206 \r\nfunction renderContent() { \r\n const content = document.getElementById('content'); \r\n content.style.color = state.color; \r\n} \r\nrenderApp(); \r\n\/\/\u70b9\u51fb\u6309\u94ae\uff0c\u66f4\u6539\u5b57\u4f53\u989c\u8272 \r\ndocument.getElementById('to-blue').onclick = function () { \r\n state.color = 'rgb(0, 51, 254)'; \r\n renderApp(); \r\n} \r\ndocument.getElementById('to-pink').onclick = function () { \r\n state.color = 'rgb(247, 109, 132)'; \r\n renderApp(); \r\n} \r\n<\/pre>\n\u8fd9\u4e2a\u5e94\u7528\u975e\u5e38\u7b80\u5355\uff0c\u4f46\u662f\u5b83\u6709\u4e00\u4e2a\u95ee\u9898\uff1astate \u662f\u5171\u4eab\u72b6\u6001\uff0c\u4f46\u662f\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u4fee\u6539\u5b83\uff0c\u4e00\u65e6\u6211\u4eec\u968f\u610f\u4fee\u6539\u4e86\u8fd9\u4e2a\u72b6\u6001\uff0c\u5c31\u53ef\u4ee5\u5bfc\u81f4\u51fa\u9519\uff0c\u4f8b\u5982\uff0c\u5728 renderHeader \u91cc\u9762\uff0c\u8bbe\u7f6e state = {}\uff0c \u5bb9\u6613\u9020\u6210\u96be\u4ee5\u9884\u6599\u7684\u9519\u8bef\u3002<\/p>\n
\u4e0d\u8fc7\u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u53c8\u7684\u786e\u9700\u8981\u5171\u4eab\u72b6\u6001\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u8bbe\u7f6e\u4e00\u4e9b\u95e8\u69db\uff0c\u6bd4\u5982\uff0c\u6211\u4eec\u7ea6\u5b9a\uff0c\u4e0d\u80fd\u76f4\u63a5\u4fee\u6539\u5168\u5c40\u72b6\u6001\uff0c\u5fc5\u987b\u8981\u901a\u8fc7\u67d0\u4e2a\u9014\u7ecf\u624d\u80fd\u4fee\u6539\u3002\u4e3a\u6b64\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a changeState \u51fd\u6570\uff0c\u5168\u5c40\u72b6\u6001\u7684\u4fee\u6539\u5747\u7531\u5b83\u8d1f\u8d23\u3002<\/p>\n
\/\/\u5728 index.js \u4e2d\u7ee7\u7eed\u8ffd\u52a0\u4ee3\u7801 \r\nfunction changeState(action) { \r\n switch(action.type) { \r\n case 'CHANGE_COLOR': \r\n return { \r\n ...state, \r\n color: action.color \r\n } \r\n default: \r\n return state; \r\n } \r\n} \r\n<\/pre>\n\u6211\u4eec\u7ea6\u5b9a\u53ea\u80fd\u901a\u8fc7 changeState \u53bb\u4fee\u6539\u72b6\u6001\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570 action,\u5305\u542b type \u5b57\u6bb5\u7684\u666e\u901a\u5bf9\u8c61\uff0ctype \u5b57\u6bb5\u7528\u4e8e\u8bc6\u522b\u4f60\u7684\u64cd\u4f5c\u7c7b\u578b(\u5373\u5982\u4f55\u4fee\u6539\u72b6\u6001)\u3002<\/p>\n
\u6211\u4eec\u5e0c\u671b\u70b9\u51fb\u6309\u94ae\uff0c\u53ef\u4ee5\u4fee\u6539\u6574\u4e2a\u5e94\u7528\u7684\u5b57\u4f53\u989c\u8272\u3002<\/p>\n
\/\/\u5728 index.js \u4e2d\u7ee7\u7eed\u8ffd\u52a0\u4ee3\u7801 \r\ndocument.getElementById('to-blue').onclick = function() { \r\n let state = changeState({ \r\n type: 'CHANGE_COLOR', \r\n color: 'rgb(0, 51, 254)' \r\n }); \r\n \/\/\u72b6\u6001\u4fee\u6539\u5b8c\u4e4b\u540e\uff0c\u9700\u8981\u91cd\u65b0\u6e32\u67d3\u9875\u9762 \r\n renderApp(state); \r\n} \r\ndocument.getElementById('to-pink').onclick = function() { \r\n let state = changeState({ \r\n type: 'CHANGE_COLOR', \r\n color: 'rgb(247, 109, 132)' \r\n }); \r\n renderApp(state); \r\n} \r\n\u62bd\u79bb store\r\n<\/pre>\n\u5c3d\u7ba1\u73b0\u5728\u6211\u4eec\u7ea6\u5b9a\u4e86\u5982\u4f55\u4fee\u6539\u72b6\u6001\uff0c\u4f46\u662f state \u662f\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u6211\u4eec\u5f88\u5bb9\u6613\u5c31\u53ef\u4ee5\u4fee\u6539\u5b83\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u5c06\u5176\u53d8\u6210\u5c40\u90e8\u53d8\u91cf\uff0c\u5c06\u5176\u5b9a\u4e49\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u90e8(createStore)\uff0c\u4f46\u662f\u5728\u5916\u90e8\u8fd8\u9700\u8981\u4f7f\u7528 state\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u63d0\u4f9b\u4e00\u4e2a\u65b9\u6cd5 getState()\uff0c\u4ee5\u4fbf\u6211\u4eec\u5728 createStore \u83b7\u53d6\u5230 state\u3002<\/p>\n
function createStore (state) { \r\n const getState = () => state; \r\n return { \r\n getState \r\n } \r\n} \r\n<\/pre>\n\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 store.getState() \u65b9\u6cd5\u53bb\u83b7\u53d6\u72b6\u6001(\u8fd9\u91cc\u9700\u8981\u8bf4\u660e\u7684\u662f\uff0cstate \u901a\u5e38\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u56e0\u6b64\u8fd9\u4e2a\u5bf9\u8c61\u5728\u5916\u90e8\u5176\u5b9e\u662f\u53ef\u4ee5\u88ab\u76f4\u63a5\u4fee\u6539\u7684\uff0c\u4f46\u662f\u5982\u679c\u6df1\u62f7\u8d1d state \u8fd4\u56de\uff0c\u90a3\u4e48\u5728\u5916\u90e8\u5c31\u4e00\u5b9a\u4fee\u6539\u4e0d\u4e86\uff0c\u9274\u4e8e redux \u6e90\u7801\u4e2d\u5c31\u662f\u76f4\u63a5\u8fd4\u56de\u4e86 state\uff0c\u6b64\u5904\u6211\u4eec\u4e5f\u4e0d\u8fdb\u884c\u6df1\u62f7\u8d1d\uff0c\u6bd5\u7adf\u8017\u8d39\u6027\u80fd)\u3002<\/p>\n
\u4ec5\u4ec5\u83b7\u53d6\u72b6\u6001\u662f\u8fdc\u8fdc\u4e0d\u591f\u7684\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u6709\u4fee\u6539\u72b6\u6001\u7684\u65b9\u6cd5\uff0c\u73b0\u5728\u72b6\u6001\u662f\u79c1\u6709\u53d8\u91cf\uff0c\u6211\u4eec\u5fc5\u987b\u8981\u5c06\u4fee\u6539\u72b6\u6001\u7684\u65b9\u6cd5\u4e5f\u653e\u5230 createStore \u4e2d\uff0c\u5e76\u5c06\u5176\u66b4\u9732\u7ed9\u5916\u90e8\u4f7f\u7528\u3002<\/p>\n
function createStore (state) { \r\n const getState = () => state; \r\n const changeState = () => { \r\n \/\/...changeState \u4e2d\u7684 code \r\n } \r\n return { \r\n getState, \r\n changeState \r\n } \r\n} \r\n<\/pre>\n\u73b0\u5728\uff0cindex.js \u4e2d\u4ee3\u7801\u53d8\u6210\u4e0b\u9762\u8fd9\u6837(to-redux\/src\/index2.js):<\/p>\n
function createStore() { \r\n let state = { \r\n color: 'blue' \r\n } \r\n const getState = () => state; \r\n function changeState(action) { \r\n switch (action.type) { \r\n case 'CHANGE_COLOR': \r\n state = { \r\n ...state, \r\n color: action.color \r\n } \r\n return state; \r\n default: \r\n return state; \r\n } \r\n } \r\n return { \r\n getState, \r\n changeState \r\n } \r\n} \r\nfunction renderApp(state) { \r\n renderHeader(state); \r\n renderContent(state); \r\n} \r\nfunction renderHeader(state) { \r\n const header = document.getElementById('header'); \r\n header.style.color = state.color; \r\n} \r\nfunction renderContent(state) { \r\n const content = document.getElementById('content'); \r\n content.style.color = state.color; \r\n} \r\ndocument.getElementById('to-blue').onclick = function () { \r\n store.changeState({ \r\n type: 'CHANGE_COLOR', \r\n color: 'rgb(0, 51, 254)' \r\n }); \r\n renderApp(store.getState()); \r\n} \r\ndocument.getElementById('to-pink').onclick = function () { \r\n store.changeState({ \r\n type: 'CHANGE_COLOR', \r\n color: 'rgb(247, 109, 132)' \r\n }); \r\n renderApp(store.getState()); \r\n} \r\nconst store = createStore(); \r\nrenderApp(store.getState()); \r\n<\/pre>\n\u5c3d\u7ba1\uff0c\u6211\u4eec\u73b0\u5728\u62bd\u79bb\u4e86 createStore \u65b9\u6cd5\uff0c\u4f46\u662f\u663e\u7136\u8fd9\u4e2a\u65b9\u6cd5\u4e00\u70b9\u90fd\u4e0d\u901a\u7528\uff0cstate \u548c changeState \u65b9\u6cd5\u90fd\u5b9a\u4e49\u5728\u4e86 createStore \u4e2d\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5176\u5b83\u5e94\u7528\u65e0\u6cd5\u590d\u7528\u6b64\u6a21\u5f0f\u3002<\/p>\n
changeState \u7684\u903b\u8f91\u7406\u5e94\u5728\u5916\u90e8\u5b9a\u4e49\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u5e94\u7528\u4fee\u6539\u72b6\u6001\u7684\u903b\u8f91\u5b9a\u7136\u662f\u4e0d\u540c\u7684\u3002\u6211\u4eec\u5c06\u8fd9\u90e8\u5206\u903b\u8f91\u5265\u79bb\u5230\u5916\u90e8\uff0c\u5e76\u5c06\u5176\u91cd\u547d\u540d\u4e3a reducer (\u618b\u95ee\u4e3a\u4ec0\u4e48\u53eb reducer\uff0c\u95ee\u5c31\u662f\u4e3a\u4e86\u548c redux \u4fdd\u6301\u4e00\u81f4)\u3002reducer \u662f\u5e72\u561b\u7684\u5462\uff0c\u8bf4\u767d\u4e86\u5c31\u662f\u6839\u636e action \u7684\u7c7b\u578b\uff0c\u8ba1\u7b97\u51fa\u65b0\u72b6\u6001\u3002\u56e0\u4e3a\u5b83\u4e0d\u662f\u5728 createStore \u5185\u90e8\u5b9a\u4e49\u7684\uff0c\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee state\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5c06\u5f53\u524d\u72b6\u6001\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5b83\u3002\u5982\u4e0b:<\/p>\n
function reducer(state, action) { \r\n switch(action.type) { \r\n case 'CHANGE_COLOR': \r\n return { \r\n ...state, \r\n color: action.color \r\n } \r\n default: \r\n return state; \r\n } \r\n} \r\ncreateStore \u8fdb\u5316\u7248\r\n\r\nfunction createStore(reducer) { \r\n let state = { \r\n color: 'blue' \r\n } \r\n const getState = () => state; \r\n \/\/\u5c06\u6b64\u5904\u7684 changeState \u66f4\u540d\u4e3a `dispatch` \r\n const dispatch = (action) => { \r\n \/\/reducer \u63a5\u6536\u8001\u72b6\u6001\u548caction\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u72b6\u6001 \r\n state = reducer(state, action); \r\n } \r\n return { \r\n getState, \r\n dispatch \r\n } \r\n} \r\n<\/pre>\n\u4e0d\u540c\u5e94\u7528\u7684 state \u5b9a\u7136\u662f\u4e0d\u540c\u7684\uff0c\u6211\u4eec\u5c06 state \u7684\u503c\u5b9a\u4e49\u5728 createStore \u5185\u90e8\u5fc5\u7136\u662f\u4e0d\u5408\u7406\u7684\u3002<\/p>\n
function createStore(reducer) { \r\n let state; \r\n const getState = () => state; \r\n const dispatch = (action) => { \r\n \/\/reducer(state, action) \u8fd4\u56de\u4e00\u4e2a\u65b0\u72b6\u6001 \r\n state = reducer(state, action); \r\n } \r\n return { \r\n getState, \r\n dispatch \r\n } \r\n} \r\n<\/pre>\n\u5927\u5bb6\u6ce8\u610f reducer \u7684\u5b9a\u4e49\uff0c\u5728\u78b0\u5230\u4e0d\u80fd\u8bc6\u522b\u7684\u52a8\u4f5c\u65f6\uff0c\u662f\u76f4\u63a5\u8fd4\u56de\u65e7\u72b6\u6001\u7684\uff0c\u73b0\u5728\uff0c\u6211\u4eec\u5229\u7528\u8fd9\u4e00\u70b9\u6765\u8fd4\u56de\u521d\u59cb\u72b6\u6001\u3002<\/p>\n
\u8981\u60f3 state \u6709\u521d\u59cb\u72b6\u6001\uff0c\u5176\u5b9e\u5f88\u7b80\u5355\uff0c\u54b1\u4eec\u5c06\u521d\u59cb\u7684 state \u7684\u521d\u59cb\u5316\u503c\u4f5c\u4e3a reducer \u7684\u53c2\u6570\u7684\u9ed8\u8ba4\u503c\uff0c\u7136\u540e\u5728 createStore \u4e2d\u6d3e\u53d1\u4e00\u4e2a reducer \u770b\u4e0d\u61c2\u7684\u52a8\u4f5c\u5c31\u53ef\u4ee5\u4e86\u3002\u8fd9\u6837 getState \u9996\u6b21\u8c03\u7528\u65f6\uff0c\u53ef\u4ee5\u83b7\u53d6\u5230\u72b6\u6001\u7684\u9ed8\u8ba4\u503c\u3002<\/p>\n
createStore \u8fdb\u5316\u72482.0<\/p>\n
function createStore(reducer) { \r\n let state; \r\n const getState = () => state; \r\n \/\/\u6bcf\u5f53 `dispatch` \u4e00\u4e2a\u52a8\u4f5c\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u8c03\u7528 `reducer` \u4ee5\u8fd4\u56de\u4e00\u4e2a\u65b0\u72b6\u6001 \r\n const dispatch = (action) => { \r\n \/\/reducer(state, action) \u8fd4\u56de\u4e00\u4e2a\u65b0\u72b6\u6001 \r\n state = reducer(state, action); \r\n } \r\n \/\/\u4f60\u8981\u662f\u6709\u4e2a action \u7684 type \u7684\u503c\u662f `@@redux\/__INIT__${Math.random()}`\uff0c\u6211\u656c\u4f60\u662f\u4e2a\u72e0\u4eba \r\n dispatch({ type: `@@redux\/__INIT__${Math.random()}` }); \r\n return { \r\n getState, \r\n dispatch \r\n } \r\n} \r\n<\/pre>\n\u73b0\u5728\u8fd9\u4e2a createStore \u5df2\u7ecf\u53ef\u4ee5\u5230\u5904\u4f7f\u7528\u4e86, \u4f46\u662f\u4f60\u6709\u6ca1\u6709\u89c9\u5f97\u6bcf\u6b21 dispatch \u540e\uff0c\u90fd\u624b\u52a8 renderApp() \u663e\u5f97\u5f88\u8822\uff0c\u5f53\u524d\u5e94\u7528\u4e2d\u662f\u8c03\u7528\u4e24\u6b21\uff0c\u5982\u679c\u9700\u8981\u4fee\u65391000\u6b21 state \u5462\uff0c\u96be\u9053\u624b\u52a8\u8c03\u7528 1000\u6b21 renderApp() \uff1f<\/p>\n
\u80fd\u4e0d\u80fd\u7b80\u5316\u4e00\u4e0b\u5462\uff1f\u6bcf\u6b21\u6570\u636e\u53d8\u5316\u7684\u65f6\u5019\uff0c\u81ea\u52a8\u8c03\u7528 renderApp()\u3002\u5f53\u7136\u6211\u4eec\u4e0d\u53ef\u80fd\u5c06 renderApp() \u5199\u5728 createStore() \u7684 dispatch \u4e2d\uff0c\u56e0\u4e3a\u5176\u5b83\u7684\u5e94\u7528\u4e2d\uff0c\u51fd\u6570\u540d\u672a\u5fc5\u53eb renderApp()\uff0c\u800c\u4e14\u6709\u53ef\u80fd\u4e0d\u6b62\u8981\u89e6\u53d1 renderApp()\u3002\u8fd9\u91cc\u53ef\u4ee5\u5f15\u5165 \u53d1\u5e03\u8ba2\u9605\u6a21\u5f0f\uff0c\u5f53\u72b6\u6001\u53d8\u5316\u65f6\uff0c\u901a\u77e5\u6240\u6709\u7684\u8ba2\u9605\u8005\u3002<\/p>\n
createStore \u8fdb\u5316\u72483.0<\/p>\n
function createStore(reducer) { \r\n let state; \r\n let listeners = []; \r\n const getState = () => state; \r\n \/\/subscribe \u6bcf\u6b21\u8c03\u7528\uff0c\u90fd\u4f1a\u8fd4\u56de\u4e00\u4e2a\u53d6\u6d88\u8ba2\u9605\u7684\u65b9\u6cd5 \r\n const subscribe = (ln) => { \r\n listeners.push(ln); \r\n \/\/\u8ba2\u9605\u4e4b\u540e\uff0c\u4e5f\u8981\u5141\u8bb8\u53d6\u6d88\u8ba2\u9605\u3002 \r\n \/\/\u96be\u9053\u6211\u8ba2\u4e86\u67d0\u672c\u6742\u5fd7\u4e4b\u540e\uff0c\u5c31\u4e0d\u5141\u8bb8\u6211\u9000\u8ba2\u5417\uff1f\u53ef\u6015~ \r\n const unsubscribe = () => { \r\n listenerslisteners = listeners.filter(listener => ln !== listener); \r\n } \r\n return unsubscribe; \r\n }; \r\n const dispatch = (action) => { \r\n \/\/reducer(state, action) \u8fd4\u56de\u4e00\u4e2a\u65b0\u72b6\u6001 \r\n state = reducer(state, action); \r\n listeners.forEach(ln => ln()); \r\n } \r\n \/\/\u4f60\u8981\u662f\u6709\u4e2a action \u7684 type \u7684\u503c\u6b63\u597d\u548c `@@redux\/__INIT__${Math.random()}` \u76f8\u7b49\uff0c\u6211\u656c\u4f60\u662f\u4e2a\u72e0\u4eba \r\n dispatch({ type: `@@redux\/__INIT__${Math.random()}` }); \r\n return { \r\n getState, \r\n dispatch, \r\n subscribe \r\n } \r\n} \r\n<\/pre>\n\u81f3\u6b64\uff0c\u4e00\u4e2a\u6700\u4e3a\u7b80\u5355\u7684 redux \u5df2\u7ecf\u521b\u5efa\u597d\u4e86\uff0ccreateStore \u662f redux \u7684\u6838\u5fc3\u3002\u6211\u4eec\u6765\u4f7f\u7528\u8fd9\u4e2a\u7cbe\u7b80\u7248\u7684 redux \u91cd\u5199\u6211\u4eec\u7684\u4ee3\u7801\uff0cindex.js \u6587\u4ef6\u5185\u5bb9\u66f4\u65b0\u5982\u4e0b(to-redux\/src\/index.js):<\/p>\n
function createStore() { \r\n \/\/code(\u81ea\u884c\u5c06\u4e0a\u9762createStore\u7684\u4ee3\u7801\u62f7\u8d1d\u81f3\u6b64\u5904) \r\n} \r\nconst initialState = { \r\n color: 'blue' \r\n} \r\nfunction reducer(state = initialState, action) { \r\n switch (action.type) { \r\n case 'CHANGE_COLOR': \r\n return { \r\n ...state, \r\n color: action.color \r\n } \r\n default: \r\n return state; \r\n } \r\n} \r\nconst store = createStore(reducer); \r\nfunction renderApp(state) { \r\n renderHeader(state); \r\n renderContent(state); \r\n} \r\nfunction renderHeader(state) { \r\n const header = document.getElementById('header'); \r\n header.style.color = state.color; \r\n} \r\nfunction renderContent(state) { \r\n const content = document.getElementById('content'); \r\n content.style.color = state.color; \r\n} \r\ndocument.getElementById('to-blue').onclick = function () { \r\n store.dispatch({ \r\n type: 'CHANGE_COLOR', \r\n color: 'rgb(0, 51, 254)' \r\n }); \r\n} \r\ndocument.getElementById('to-pink').onclick = function () { \r\n store.dispatch({ \r\n type: 'CHANGE_COLOR', \r\n color: 'rgb(247, 109, 132)' \r\n }); \r\n} \r\nrenderApp(store.getState()); \r\n\/\/\u6bcf\u6b21state\u53d1\u751f\u6539\u53d8\u65f6\uff0c\u90fd\u91cd\u65b0\u6e32\u67d3 \r\nstore.subscribe(() => renderApp(store.getState())); \r\n<\/pre>\n\u5982\u679c\u73b0\u5728\u6211\u4eec\u73b0\u5728\u5e0c\u671b\u5728\u70b9\u51fb\u5b8c Pink \u4e4b\u540e\uff0c\u5b57\u4f53\u8272\u4e0d\u5141\u8bb8\u4fee\u6539\uff0c\u90a3\u4e48\u6211\u4eec\u8fd8\u53ef\u4ee5\u53d6\u6d88\u8ba2\u9605:<\/p>\n
const unsub = store.subscribe(() => renderApp(store.getState())); \r\ndocument.getElementById('to-pink').onclick = function () { \r\n \/\/code... \r\n unsub(); \/\/\u53d6\u6d88\u8ba2\u9605 \r\n} \r\n<\/pre>\n\u987a\u4fbf\u8bf4\u4e00\u53e5: reducer \u662f\u4e00\u4e2a\u7eaf\u51fd\u6570(\u7eaf\u51fd\u6570\u7684\u6982\u5ff5\u5982\u679c\u4e0d\u4e86\u89e3\u7684\u8bdd\uff0c\u81ea\u884c\u67e5\u9605\u8d44\u6599)\uff0c\u5b83\u63a5\u6536\u5148\u524d\u7684 state \u548c action\uff0c\u5e76\u8fd4\u56de\u65b0\u7684 state\u3002\u4e0d\u8981\u95ee\u4e3a\u4ec0\u4e48 action \u4e2d\u4e00\u5b9a\u8981\u6709 type \u5b57\u6bb5\uff0c\u8fd9\u4ec5\u4ec5\u662f\u4e00\u4e2a\u7ea6\u5b9a\u800c\u5df2(redux \u5c31\u662f\u8fd9\u4e48\u8bbe\u8ba1\u7684)<\/p>\n
\u9057\u7559\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48 reducer \u4e00\u5b9a\u8981\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 state\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u4fee\u6539 state \u5462\u3002\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u7559\u4e0b\u4f60\u7684\u7b54\u6848\u3002<\/p>\n
\u524d\u9762\u6211\u4eec\u4e00\u6b65\u4e00\u6b65\u63a8\u6f14\u4e86 redux \u7684\u6838\u5fc3\u4ee3\u7801\uff0c\u73b0\u5728\u6211\u4eec\u6765\u56de\u987e\u4e00\u4e0b redux \u7684\u8bbe\u8ba1\u601d\u60f3:<\/p>\n
Redux \u8bbe\u8ba1\u601d\u60f3<\/p>\n
Redux \u5c06\u6574\u4e2a\u5e94\u7528\u72b6\u6001(state)\u5b58\u50a8\u5230\u4e00\u4e2a\u5730\u65b9(\u901a\u5e38\u6211\u4eec\u79f0\u5176\u4e3a store)
\n\u5f53\u6211\u4eec\u9700\u8981\u4fee\u6539\u72b6\u6001\u65f6\uff0c\u5fc5\u987b\u6d3e\u53d1(dispatch)\u4e00\u4e2a action( action \u662f\u4e00\u4e2a\u5e26\u6709 type \u5b57\u6bb5\u7684\u5bf9\u8c61)
\n\u4e13\u95e8\u7684\u72b6\u6001\u5904\u7406\u51fd\u6570 reducer \u63a5\u6536\u65e7\u7684 state \u548c action \uff0c\u5e76\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 state
\n\u901a\u8fc7 subscribe \u8bbe\u7f6e\u8ba2\u9605\uff0c\u6bcf\u6b21\u6d3e\u53d1\u52a8\u4f5c\u65f6\uff0c\u901a\u77e5\u6240\u6709\u7684\u8ba2\u9605\u8005\u3002
\n\u54b1\u4eec\u73b0\u5728\u5df2\u7ecf\u6709\u4e00\u4e2a\u57fa\u7840\u7248\u672c\u7684 redux \u4e86\uff0c\u4f46\u662f\u5b83\u8fd8\u4e0d\u80fd\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u6c42\u3002\u6211\u4eec\u5e73\u65f6\u7684\u4e1a\u52a1\u5f00\u53d1\u4e0d\u4f1a\u50cf\u4e0a\u9762\u6240\u5199\u7684\u793a\u4f8b\u90a3\u6837\u7b80\u5355\uff0c\u90a3\u4e48\u5c31\u4f1a\u6709\u4e00\u4e2a\u95ee\u9898: reducer \u51fd\u6570\u53ef\u80fd\u4f1a\u975e\u5e38\u957f\uff0c\u56e0\u4e3a action \u7684\u7c7b\u578b\u4f1a\u975e\u5e38\u591a\u3002\u8fd9\u6837\u80af\u5b9a\u662f\u4e0d\u5229\u4e8e\u4ee3\u7801\u7684\u7f16\u5199\u548c\u9605\u8bfb\u7684\u3002<\/p>\n
\u8bd5\u60f3\u4e00\u4e0b\uff0c\u4f60\u7684\u4e1a\u52a1\u4e2d\u6709\u4e00\u767e\u79cd action \u9700\u8981\u5904\u7406\uff0c\u628a\u8fd9\u4e00\u767e\u79cd\u60c5\u51b5\u7f16\u5199\u5728\u4e00\u4e2a reducer \u4e2d\uff0c\u4e0d\u4ec5\u5199\u5f97\u4eba\u6076\u5fc3\uff0c\u540e\u671f\u7ef4\u62a4\u4ee3\u7801\u7684\u540c\u4e8b\u66f4\u662f\u60f3\u6740\u4eba\u3002<\/p>\n
\u56e0\u6b64\uff0c\u6211\u4eec\u6700\u597d\u5355\u72ec\u7f16\u5199 reducer\uff0c\u7136\u540e\u5bf9 reducer \u8fdb\u884c\u5408\u5e76\u3002\u6709\u8bf7\u6211\u4eec\u7684 combineReducers(\u548c redux \u5e93\u7684\u547d\u540d\u4fdd\u6301\u4e00\u81f4) \u95ea\u4eae\u767b\u573a~<\/p>\n
combineReducers\r\n<\/pre>\n\u9996\u5148\u6211\u4eec\u9700\u8981\u660e\u786e\u4e00\u70b9\uff1acombineReducers \u53ea\u662f\u4e00\u4e2a\u5de5\u5177\u51fd\u6570\uff0c\u6b63\u5982\u6211\u4eec\u524d\u9762\u6240\u8bf4\uff0c\u5b83\u5c06\u591a\u4e2a reducer \u5408\u5e76\u4e3a\u4e00\u4e2a reducer\u3002combineReducers \u8fd4\u56de\u7684\u662f reducer\uff0c\u4e5f\u5c31\u662f\u8bf4\u5b83\u662f\u4e00\u4e2a\u9ad8\u9636\u51fd\u6570\u3002<\/p>\n
\u6211\u4eec\u8fd8\u662f\u4ee5\u4e00\u4e2a\u793a\u4f8b\u6765\u8bf4\u660e\uff0c\u5c3d\u7ba1 redux \u4e0d\u662f\u975e\u5f97\u548c react \u914d\u5408\uff0c\u4e0d\u8fc7\u9274\u4e8e\u5176\u4e0e react \u914d\u5408\u6700\u4e3a\u9002\u5408\uff0c\u6b64\u5904\uff0c\u4ee5 react \u4ee3\u7801\u4e3a\u4f8b\uff1a<\/p>\n
\u8fd9\u4e00\u6b21\u9664\u4e86\u4e0a\u9762\u7684\u5c55\u793a\u4ee5\u5916\uff0c\u6211\u4eec\u65b0\u589e\u4e86\u4e00\u4e2a\u8ba1\u6570\u5668\u529f\u80fd( \u4f7f\u7528 React \u91cd\u6784 ===> to-redux2):<\/p>\n
\/\/\u73b0\u5728\u6211\u4eec\u7684 state \u7ed3\u6784\u5982\u4e0b: \r\nlet state = { \r\n theme: { \r\n color: 'blue' \r\n }, \r\n counter: { \r\n number: 0 \r\n } \r\n} \r\n<\/pre>\n\u663e\u7136\uff0c\u4fee\u6539\u4e3b\u9898\u548c\u8ba1\u6570\u5668\u662f\u53ef\u4ee5\u5206\u5272\u5f00\u5f97\uff0c\u7531\u4e0d\u540c\u7684 reducer \u53bb\u5904\u7406\u662f\u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n
store\/reducers\/counter.js \r\n\u8d1f\u8d23\u5904\u7406\u8ba1\u6570\u5668\u7684state\u3002\r\n\r\nimport { INCRENENT, DECREMENT } from '..\/action-types'; \r\nexport default counter(state = {number: 0}, action) { \r\n switch (action.type) { \r\n case INCRENENT: \r\n return { \r\n ...state, \r\n number: state.number + action.number \r\n } \r\n case DECREMENT: \r\n return { \r\n ...state, \r\n number: state.number - action.number \r\n } \r\n default: \r\n return state; \r\n } \r\n} \r\nstore\/reducers\/theme.js \r\n<\/pre>\n\u8d1f\u8d23\u5904\u7406\u4fee\u6539\u4e3b\u9898\u8272\u7684state\u3002<\/p>\n
import { CHANGE_COLOR } from '..\/action-types'; \r\nexport default function theme(state = {color: 'blue'}, action) { \r\n switch (action.type) { \r\n case CHANGE_COLOR: \r\n return { \r\n ...state, \r\n color: action.color \r\n } \r\n default: \r\n return state; \r\n } \r\n} \r\n<\/pre>\n\u6bcf\u4e2a reducer \u53ea\u8d1f\u8d23\u7ba1\u7406\u5168\u5c40 state \u4e2d\u5b83\u8d1f\u8d23\u7684\u4e00\u90e8\u5206\u3002\u6bcf\u4e2a reducer \u7684 state \u53c2\u6570\u90fd\u4e0d\u540c\uff0c\u5206\u522b\u5bf9\u5e94\u5b83\u7ba1\u7406\u7684\u90a3\u90e8\u5206 state \u6570\u636e\u3002<\/p>\n
import counter from '.\/counter'; \r\nimport theme from '.\/theme'; \r\nexport default function appReducer(state={}, action) { \r\n return { \r\n theme: theme(state.theme, action), \r\n counter: counter(state.counter, action) \r\n } \r\n} \r\n<\/pre>\nappReducer \u5373\u662f\u5408\u5e76\u4e4b\u540e\u7684 reducer\uff0c\u4f46\u662f\u5f53 reducer \u8f83\u591a\u65f6\uff0c\u8fd9\u6837\u5199\u4e5f\u663e\u5f97\u7e41\u7410\uff0c\u56e0\u6b64\u6211\u4eec\u7f16\u5199\u4e00\u4e2a\u5de5\u5177\u51fd\u6570\u6765\u751f\u6210\u8fd9\u6837\u7684 appReducer\uff0c\u6211\u4eec\u628a\u8fd9\u4e2a\u5de5\u5177\u51fd\u6570\u547d\u540d\u4e3a combineReducers\u3002<\/p>\n
\u6211\u4eec\u6765\u5c1d\u8bd5\u4e00\u4e0b\u7f16\u5199\u8fd9\u4e2a\u5de5\u5177\u51fd\u6570 combineReducers:<\/p>\n
\u601d\u8def\uff1a<\/p>\n
combineReducers \u8fd4\u56de reducer\r\n combineReducers \u7684\u5165\u53c2\u662f\u591a\u4e2a reducer \u7ec4\u6210\u7684\u5bf9\u8c61\r\n \u6bcf\u4e2a reducer \u53ea\u5904\u7406\u5168\u5c40 state \u4e2d\u81ea\u5df1\u8d1f\u8d23\u7684\u90e8\u5206 \r\n\/\/reducers \u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5c5e\u6027\u503c\u662f\u6bcf\u4e00\u4e2a\u62c6\u5206\u7684 reducer \r\nexport default function combineReducers(reducers) { \r\n return function combination(state={}, action) { \r\n \/\/reducer \u7684\u8fd4\u56de\u503c\u662f\u65b0\u7684 state \r\n let newState = {}; \r\n for(var key in reducers) { \r\n newState[key] = reducers[key](state[key], action); \r\n } \r\n return newState; \r\n } \r\n} \r\n<\/pre>\nreducer \u5c06\u8d1f\u8d23\u8fd4\u56de state \u7684\u9ed8\u8ba4\u503c\u3002\u6bd4\u5982\u672c\u4f8b\u4e2d\uff0ccreateStore \u4e2d dispatch({type:@@redux\/__INIT__${Math.random()}})\uff0c\u800c\u4f20\u9012\u7ed9 createStore \u7684\u662f combineReducers(reducers) \u8fd4\u56de\u7684\u51fd\u6570 combination\u3002<\/p>\n
\u6839\u636e state=reducer(state,action)\uff0cnewState.theme=theme(undefined, action), newState.counter=counter(undefined, action)\uff0ccounter \u548c theme \u4e24\u4e2a\u5b50 reducer \u5206\u522b\u8fd4\u56de newState.theme \u548c newState.counter \u7684\u521d\u59cb\u503c\u3002<\/p>\n
\u5229\u7528\u6b64 combineReducers \u53ef\u4ee5\u91cd\u5199 store\/reducers\/index.js<\/p>\n
import counter from '.\/counter'; \r\nimport theme from '.\/theme'; \r\nimport { combineReducers } from '..\/redux'; \r\n\/\/\u660e\u663e\u7b80\u6d01\u4e86\u8bb8\u591a~ \r\nexport default combineReducers({ \r\n counter, \r\n theme \r\n}); \r\n<\/pre>\n\u6211\u4eec\u5199\u7684 combineReducers \u867d\u7136\u770b\u8d77\u6765\u5df2\u7ecf\u80fd\u591f\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u6c42\uff0c\u4f46\u662f\u5176\u6709\u4e00\u4e2a\u7f3a\u70b9\uff0c\u5373\u6bcf\u6b21\u90fd\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 state \u5bf9\u8c61\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u5728\u6570\u636e\u6ca1\u6709\u53d8\u5316\u65f6\u8fdb\u884c\u65e0\u610f\u4e49\u7684\u91cd\u65b0\u6e32\u67d3\u3002\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5bf9\u6570\u636e\u8fdb\u884c\u5224\u65ad\uff0c\u5728\u6570\u636e\u6ca1\u6709\u53d8\u5316\u65f6\uff0c\u8fd4\u56de\u539f\u672c\u7684 state \u5373\u53ef\u3002<\/p>\n
combineReducers \u8fdb\u5316\u7248<\/p>\n
\/\/\u4ee3\u7801\u4e2d\u7701\u7565\u4e86\u4e00\u4e9b\u5224\u65ad\uff0c\u9ed8\u8ba4\u4f20\u9012\u7684\u53c2\u6570\u5747\u662f\u7b26\u5408\u8981\u6c42\u7684\uff0c\u6709\u5174\u8da3\u53ef\u4ee5\u67e5\u770b\u6e90\u7801\u4e2d\u5bf9\u53c2\u6570\u5408\u6cd5\u6027\u7684\u5224\u65ad\u53ca\u5904\u7406 \r\nexport default function combineReducers(reducers) { \r\n return function combination(state={}, action) { \r\n let nextState = {}; \r\n let hasChanged = false; \/\/\u72b6\u6001\u662f\u5426\u6539\u53d8 \r\n for(let key in reducers) { \r\n const previousStateForKey = state[key]; \r\n const nextStateForKey = reducers[key](previousStateForKey, action); \r\n nextState[key] = nextStateForKey; \r\n \/\/\u53ea\u6709\u6240\u6709\u7684 nextStateForKey \u5747\u4e0e previousStateForKey \u76f8\u7b49\u65f6\uff0chasChanged \u7684\u503c\u624d\u662f false \r\n hasChangedhasChanged = hasChanged || nextStateForKey !== previousStateForKey; \r\n } \r\n \/\/state \u6ca1\u6709\u6539\u53d8\u65f6\uff0c\u8fd4\u56de\u539f\u5bf9\u8c61 \r\n return hasChanged ? nextState : state; \r\n } \r\n} \r\napplyMiddleware\r\n<\/pre>\n\u5b98\u65b9\u6587\u6863\u4e2d\uff0c\u5173\u4e8e applyMiddleware \u7684\u89e3\u91ca\u5f88\u6e05\u695a\uff0c\u4e0b\u9762\u7684\u5185\u5bb9\u4e5f\u53c2\u8003\u4e86\u5b98\u65b9\u6587\u6863\u7684\u5185\u5bb9:<\/p>\n
\u65e5\u5fd7\u8bb0\u5f55<\/p>\n
\u8003\u8651\u4e00\u4e2a\u5c0f\u5c0f\u7684\u95ee\u9898\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u6bcf\u6b21\u72b6\u6001\u6539\u53d8\u524d\u80fd\u591f\u5728\u63a7\u5236\u53f0\u4e2d\u6253\u5370\u51fa state\uff0c\u90a3\u4e48\u6211\u4eec\u8981\u600e\u4e48\u505a\u5462\uff1f<\/p>\n
\u6700\u7b80\u5355\u7684\u5373\u662f:
\n
\n\u5f53\u7136\uff0c\u8fd9\u79cd\u65b9\u5f0f\u80af\u5b9a\u662f\u4e0d\u53ef\u53d6\u7684\uff0c\u5982\u679c\u6211\u4eec\u4ee3\u7801\u4e2d\u6d3e\u53d1100\u6b21\uff0c\u6211\u4eec\u4e0d\u53ef\u80fd\u8fd9\u6837\u5199\u4e00\u767e\u6b21\u3002\u65e2\u7136\u662f\u72b6\u6001\u6539\u53d8\u65f6\u6253\u5370 state\uff0c\u4e5f\u662f\u8bf4\u662f\u5728 dispatch \u4e4b\u524d\u6253\u5370 state, \u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u91cd\u5199 store.dispatch \u65b9\u6cd5\uff0c\u5728\u6d3e\u53d1\u524d\u6253\u5370 state \u5373\u53ef\u3002<\/p>\n
let store = createStore(reducer); \r\nconst next = store.dispatch; \/\/next \u7684\u547d\u4ee4\u662f\u4e3a\u4e86\u548c\u4e2d\u95f4\u4ef6\u7684\u6e90\u7801\u4e00\u81f4 \r\nstore.dispatch = action => { \r\n console.log(store.getState()); \r\n next(action); \r\n} \r\n<\/pre>\n\u5d29\u6e83\u4fe1\u606f<\/p>\n
\u5047\u8bbe\u6211\u4eec\u4e0d\u4ec5\u4ec5\u9700\u8981\u6253\u5370 state\uff0c\u8fd8\u9700\u8981\u5728\u6d3e\u53d1\u5f02\u5e38\u51fa\u9519\u65f6\uff0c\u6253\u5370\u51fa\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n
const next = store.dispatch; \/\/next \u7684\u547d\u540d\u662f\u4e3a\u4e86\u548c\u4e2d\u95f4\u4ef6\u7684\u6e90\u7801\u4e00\u81f4 \r\nstore.dispatch = action => { \r\n try{ \r\n console.log(store.getState()); \r\n next(action); \r\n } catct(err) { \r\n console.error(err); \r\n } \r\n} \r\n<\/pre>\n\u800c\u5982\u679c\u6211\u4eec\u8fd8\u6709\u5176\u4ed6\u7684\u9700\u6c42\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u4e0d\u505c\u7684\u4fee\u6539 store.dispatch \u65b9\u6cd5\uff0c\u6700\u540e\u5bfc\u81f4\u8fd9\u4e2a\u8fd9\u90e8\u5206\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u3002<\/p>\n
\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5206\u79bb loggerMiddleware \u548c exceptionMiddleware.<\/p>\n
let store = createStore(reducer); \r\nconst next = store.dispatch; \/\/next \u7684\u547d\u540d\u662f\u4e3a\u4e86\u548c\u4e2d\u95f4\u4ef6\u7684\u6e90\u7801\u4e00\u81f4 \r\nconst loggerMiddleware = action => { \r\n console.log(store.getState()); \r\n next(action); \r\n} \r\nconst exceptionMiddleware = action => { \r\n try{ \r\n loggerMiddleware(action); \r\n }catch(err) { \r\n console.error(err); \r\n } \r\n} \r\nstore.dispatch = exceptionMiddleware; \r\n<\/pre>\n\u6211\u4eec\u77e5\u9053\uff0c\u5f88\u591a middleware \u90fd\u662f\u7b2c\u4e09\u65b9\u63d0\u4f9b\u7684\uff0c\u90a3\u4e48 store \u80af\u5b9a\u662f\u9700\u8981\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9 middleware \uff0c\u8fdb\u4e00\u6b65\u6539\u5199:<\/p>\n
const loggerMiddleware = store => action => { \r\n const next = store.dispatch; \r\n console.log(store.getState()); \r\n next(action); \r\n} \r\nconst exceptionMiddleware = store => action => { \r\n try{ \r\n loggerMiddleware(store)(action); \r\n }catch(err) { \r\n console.error(err); \r\n } \r\n} \r\n\/\/\u4f7f\u7528 \r\nstore.dispatch = exceptionMiddleware(store)(action); \r\n<\/pre>\n\u73b0\u5728\u8fd8\u6709\u4e00\u4e2a\u5c0f\u5c0f\u7684\u95ee\u9898\uff0cexceptionMiddleware \u4e2d\u7684 loggerMiddleware \u662f\u5199\u6b7b\u7684\uff0c\u8fd9\u80af\u5b9a\u662f\u4e0d\u5408\u7406\u7684\uff0c\u6211\u4eec\u5e0c\u671b\u8fd9\u662f\u4e00\u4e2a\u53c2\u6570\uff0c\u8fd9\u6837\u4f7f\u7528\u8d77\u6765\u624d\u7075\u6d3b\uff0c\u6ca1\u9053\u7406\u53ea\u6709 exceptionMiddleware \u9700\u8981\u7075\u6d3b\uff0c\u800c\u4e0d\u7ba1 loggerMiddleware\uff0c\u8fdb\u4e00\u6b65\u6539\u5199\u5982\u4e0b\uff1a<\/p>\n
const loggerMiddleware = store => next => action => { \r\n console.log(store.getState()); \r\n return next(action); \r\n} \r\nconst exceptionMiddleware = store => next => action => { \r\n try{ \r\n return next(action); \r\n }catch(err) { \r\n console.error(err); \r\n } \r\n} \r\n\/\/\u4f7f\u7528 \r\nconst next = store.dispatch; \r\nconst logger = loggerMiddleware(store); \r\nstore.dispatch = exceptionMiddleware(store)(logger(next)); \r\n<\/pre>\n\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u6709\u4e86\u901a\u7528 middleware \u7684\u7f16\u5199\u683c\u5f0f\u4e86\u3002<\/p>\n
middleware \u63a5\u6536\u4e86\u4e00\u4e2a next() \u7684 dispatch \u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a dispatch \u51fd\u6570\uff0c\u8fd4\u56de\u7684\u51fd\u6570\u4f1a\u88ab\u4f5c\u4e3a\u4e0b\u4e00\u4e2a middleware \u7684 next()<\/p>\n
\u4f46\u662f\u6709\u4e00\u4e2a\u5c0f\u5c0f\u7684\u95ee\u9898\uff0c\u5f53\u4e2d\u95f4\u4ef6\u5f88\u591a\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u4e2d\u95f4\u4ef6\u7684\u4ee3\u7801\u4f1a\u53d8\u5f97\u5f88\u7e41\u7410\u3002\u4e3a\u6b64\uff0credux \u63d0\u4f9b\u4e86\u4e00\u4e2a applyMiddleware \u7684\u5de5\u5177\u51fd\u6570\u3002<\/p>\n
\u4e0a\u9762\u6211\u4eec\u80fd\u591f\u770b\u51fa\uff0c\u5176\u5b9e\u6211\u4eec\u6700\u7ec8\u8981\u6539\u53d8\u7684\u5c31\u662f dispatch\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u91cd\u5199 store\uff0c\u8fd4\u56de\u4fee\u6539\u4e86 dispatch \u65b9\u6cd5\u4e4b\u540e\u7684 store.<\/p>\n
\u6240\u4ee5\uff0c\u6211\u4eec\u53ef\u4ee5\u660e\u786e\u4ee5\u4e0b\u51e0\u70b9:<\/p>\n
applyMiddleware \u8fd4\u56de\u503c\u662f store\r\n applyMiddleware \u80af\u5b9a\u8981\u63a5\u53d7 middleware \u4f5c\u4e3a\u53c2\u6570\r\n applyMiddleware \u8981\u63a5\u53d7 store \u4f5c\u4e3a\u5165\u53c2\uff0c\u4e0d\u8fc7 redux \u6e90\u7801\u4e2d\u5165\u53c2\u662f createStore \u548c createStore \u7684\u5165\u53c2\uff0c\u60f3\u60f3\u4e5f\u662f\uff0c\u6ca1\u6709\u5fc5\u8981\u5728\u5916\u90e8\u521b\u5efa\u51fa store\uff0c\u6bd5\u7adf\u5728\u5916\u90e8\u521b\u5efa\u51fa\u7684 store \u9664\u4e86\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u8fdb\u51fd\u6570\uff0c\u4e5f\u6ca1\u6709\u5176\u5b83\u4f5c\u7528\uff0c\u4e0d\u5982\u628a createStore \u548c createStore \u9700\u8981\u4f7f\u7528\u7684\u53c2\u6570\u4f20\u9012\u8fdb\u6765\u3002 \r\n\/\/applyMiddleWare \u8fd4\u56de store. \r\nconst applyMiddleware = middleware => createStore => (...args) => { \r\n let store = createStore(...args); \r\n let middle = loggerMiddleware(store); \r\n let dispatch = middle(store.dispatch); \/\/\u65b0\u7684dispatch\u65b9\u6cd5 \r\n \/\/\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684store---\u91cd\u5199\u4e86dispatch\u65b9\u6cd5 \r\n return { \r\n ...store, \r\n dispatch \r\n } \r\n} \r\n<\/pre>\n\u4ee5\u4e0a\u662f\u4e00\u4e2a middleware \u7684\u60c5\u51b5\uff0c\u4f46\u662f\u6211\u4eec\u77e5\u9053\uff0cmiddleware \u53ef\u80fd\u662f\u4e00\u4e2a\u6216\u8005\u662f\u591a\u4e2a\uff0c\u800c\u4e14\u6211\u4eec\u4e3b\u8981\u662f\u8981\u89e3\u51b3\u591a\u4e2a middleware \u7684\u95ee\u9898\uff0c\u8fdb\u4e00\u6b65\u6539\u5199\u3002<\/p>\n
\/\/applyMiddleware \u8fd4\u56de store. \r\nconst applyMiddleware = (...middlewares) => createStore => (...args) => { \r\n let store = createStore(...args); \r\n \/\/\u987a\u4fbf\u8bf4\u4e00\u53e5: redux \u6e90\u7801\u4e2d\u6ca1\u6709\u76f4\u63a5\u628a store \u4f20\u9012\u8fc7\u53bb\uff0c\u800c\u662f\u628a getState \u548c dispatch \u4f20\u9012\u7ed9\u4e86 middleware \r\n let middles = middlewares.map(middleware => middleware(store)); \r\n \/\/\u73b0\u5728\u6211\u4eec\u6709\u591a\u4e2a middleware\uff0c\u9700\u8981\u591a\u6b21\u589e\u5f3a dispatch \r\n let dispatch = middles.reduceRight((prev, current) => prev(current), store.dispatch); \r\n return { \r\n ...store, \r\n dispatch \r\n } \r\n} \r\n<\/pre>\n\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u4e0d\u662f\u7406\u89e3\u4e86\u4e0a\u9762\u7684 middles.reduceRight\uff0c\u4e0b\u9762\u4e3a\u5927\u5bb6\u7ec6\u81f4\u8bf4\u660e\u4e00\u4e0b\uff1a<\/p>\n
\/*\u4e09\u4e2a\u4e2d\u95f4\u4ef6*\/ \r\nlet logger1 = store => dispatch => action => { \r\n console.log('111'); \r\n dispatch(action); \r\n console.log('444'); \r\n} \r\nlet logger2 = store => dispatch => action => { \r\n console.log('222'); \r\n dispatch(action); \r\n console.log('555') \r\n} \r\nlet logger3 = store => dispatch => action => { \r\n console.log('333'); \r\n dispatch(action); \r\n console.log('666'); \r\n} \r\nlet middle1 = logger1(store); \r\nlet middle2 = logger2(store); \r\nlet middle3 = logger3(store); \r\n\/\/applyMiddleware(logger1,logger3,logger3)(createStore)(reducer) \r\n\/\/\u5982\u679c\u76f4\u63a5\u66ff\u6362 \r\nstore.dispatch = middle1(middle2(middle3(store.dispatch))); \r\n<\/pre>\n\u89c2\u5bdf\u4e0a\u9762\u7684 middle1(middle2(middle3(store.dispatch)))\uff0c\u5982\u679c\u6211\u4eec\u628a middle1,middle2,middle3 \u770b\u6210\u662f\u6570\u7ec4\u7684\u6bcf\u4e00\u9879\uff0c\u5982\u679c\u5bf9\u6570\u7ec4\u7684API\u6bd4\u8f83\u719f\u6089\u7684\u8bdd\uff0c\u53ef\u4ee5\u60f3\u5230 reduce\uff0c\u5982\u679c\u4f60\u8fd8\u4e0d\u719f\u6089 reduce\uff0c\u53ef\u4ee5\u67e5\u770bMDN\u6587\u6863\u3002<\/p>\n
\/\/applyMiddleware(logger1,logger3,logger3)(createStore)(reducer) \r\n\/\/reduceRight \u4ece\u53f3\u5230\u5de6\u6267\u884c \r\nmiddles.reduceRight((prev, current) => current(prev), store.dispatch); \r\n\/\/\u7b2c\u4e00\u6b21 prev: store.dispatch current: middle3 \r\n\/\/\u7b2c\u4e8c\u6b21 prev: middle3(store.dispatch) current: middle2 \r\n\/\/\u7b2c\u4e09\u6b21 prev: middle2(middle3(store.dispatch)) current: middle1 \r\n\/\/\u7ed3\u679c middle1(middle2(middle3(store.dispatch))) \r\n<\/pre>\n\u9605\u8bfb\u8fc7 redux \u7684\u6e90\u7801\u7684\u540c\u5b66\uff0c\u53ef\u80fd\u77e5\u9053\u6e90\u7801\u4e2d\u662f\u63d0\u4f9b\u4e86\u4e00\u4e2a compose \u51fd\u6570\uff0c\u800c compose \u51fd\u6570\u4e2d\u6ca1\u6709\u4f7f\u7528 reduceRight\uff0c\u800c\u662f\u4f7f\u7528\u7684 reduce\uff0c\u56e0\u800c\u4ee3\u7801\u7a0d\u5fae\u6709\u70b9\u4e0d\u540c\u3002\u4f46\u662f\u5206\u6790\u8fc7\u7a0b\u8fd8\u662f\u4e00\u6837\u7684\u3002<\/p>\n
compose.js\r\nexport default function compose(...funcs) { \r\n \/\/\u5982\u679c\u6ca1\u6709\u4e2d\u95f4\u4ef6 \r\n if (funcs.length === 0) { \r\n return arg => arg \r\n } \r\n \/\/\u4e2d\u95f4\u4ef6\u957f\u5ea6\u4e3a1 \r\n if (funcs.length === 1) { \r\n return funcs[0] \r\n } \r\n return funcs.reduce((prev, current) => (...args) => prev(current(...args))); \r\n} \r\n<\/pre>\n\u5173\u4e8e reduce \u7684\u5199\u6cd5\uff0c\u5efa\u8bae\u50cf\u4e0a\u9762\u7684 reduceRight \u4e00\u6837\uff0c\u8fdb\u884c\u4e00\u6b21\u5206\u6790<\/p>\n
\u4f7f\u7528 compose \u5de5\u5177\u51fd\u6570\u91cd\u5199 applyMiddleware\u3002<\/p>\n
const applyMiddleware = (...middlewares) => createStore => (...args) => { \r\n let store = createStore(...args); \r\n let middles = middlewares.map(middleware => middleware(store)); \r\n let dispatch = compose(...middles)(store.dispatch); \r\n return { \r\n ...store, \r\n dispatch \r\n } \r\n} \r\nbindActionCreators\r\n<\/pre>\nredux \u8fd8\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86 bindActionCreators \u5de5\u5177\u51fd\u6570\uff0c\u8fd9\u4e2a\u5de5\u5177\u51fd\u6570\u4ee3\u7801\u5f88\u7b80\u5355\uff0c\u6211\u4eec\u5f88\u5c11\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u5b83\uff0creact-redux \u4e2d\u4f1a\u4f7f\u7528\u5230\u3002\u6b64\u5904\uff0c\u7b80\u5355\u8bf4\u660e\u4e00\u4e0b:<\/p>\n
\/\/\u901a\u5e38\u6211\u4eec\u4f1a\u8fd9\u6837\u7f16\u5199\u6211\u4eec\u7684 actionCreator \r\nimport { INCRENENT, DECREMENT } from '..\/action-types'; \r\nconst counter = { \r\n add(number) { \r\n return { \r\n type: INCRENENT, \r\n number \r\n } \r\n }, \r\n minus(number) { \r\n return { \r\n type: DECREMENT, \r\n number \r\n } \r\n } \r\n} \r\nexport default counter; \r\n<\/pre>\n\u5728\u6d3e\u53d1\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u8fd9\u6837\u5199:<\/p>\n
import counter from 'xx\/xx'; \r\nimport store from 'xx\/xx'; \r\nstore.dispatch(counter.add()); \r\n\u5f53\u7136\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u7f16\u5199\u6211\u4eec\u7684 actionCreator:\r\n\r\nfunction add(number) { \r\n return { \r\n type: INCRENENT, \r\n number \r\n } \r\n} \r\n<\/pre>\n\u6d3e\u53d1\u65f6\uff0c\u9700\u8981\u8fd9\u6837\u7f16\u5199:<\/p>\n
store.dispatch(add(number)); \r\n\u4ee5\u4e0a\u4ee3\u7801\u6709\u4e00\u4e2a\u5171\u540c\u70b9\uff0c\u5c31\u662f\u90fd\u662f store.dispatch \u6d3e\u53d1\u4e00\u4e2a\u52a8\u4f5c\u3002\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u5c06 store.dispatch \u548c actionCreator \u7ed1\u5b9a\u8d77\u6765\u3002\r\n\r\nfunction bindActionCreator(actionCreator, dispatch) { \r\n return (...args) => dispatch(actionCreator(...args)); \r\n} \r\nfunction bindActionCreators(actionCreator, dispatch) { \r\n \/\/actionCreators \u53ef\u4ee5\u662f\u4e00\u4e2a\u666e\u901a\u51fd\u6570\u6216\u8005\u662f\u4e00\u4e2a\u5bf9\u8c61 \r\n if(typeof actionCreator === 'function') { \r\n \/\/\u5982\u679c\u662f\u51fd\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\uff0c\u8c03\u7528\u65f6\uff0cdispatch \u8fd9\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u503c \r\n bindActionCreator(actionCreator, dispatch); \r\n }else if(typeof actionCreator === 'object') { \r\n \/\/\u5982\u679c\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u90a3\u4e48\u5bf9\u8c61\u7684\u6bcf\u4e00\u9879\u90fd\u8981\u90fd\u8981\u8fd4\u56de bindActionCreator \r\n const boundActionCreators = {} \r\n for(let key in actionCreator) { \r\n boundActionCreators[key] = bindActionCreator(actionCreator[key], dispatch); \r\n } \r\n return boundActionCreators; \r\n } \r\n} \r\n<\/pre>\n\u5728\u4f7f\u7528\u65f6:<\/p>\n
let counter = bindActionCreators(counter, store.dispatch); \r\n\/\/\u6d3e\u53d1\u65f6 \r\ncounter.add(); \r\ncounter.minus(); \r\n<\/pre>\n\u8fd9\u91cc\u770b\u8d77\u6765\u5e76\u6ca1\u6709\u7cbe\u7b80\u592a\u591a\uff0c\u540e\u9762\u5728\u5206\u6790 react-redux \u65f6\uff0c\u4f1a\u8bf4\u660e\u4e3a\u4ec0\u4e48\u9700\u8981\u8fd9\u4e2a\u5de5\u5177\u51fd\u6570\u3002<\/p>\n
\u81f3\u6b64\uff0c\u6211\u7684 redux \u57fa\u672c\u5df2\u7ecf\u7f16\u5199\u5b8c\u6bd5\u3002\u4e0e redux \u7684\u6e90\u7801\u76f8\u6bd4\uff0c\u8fd8\u76f8\u5dee\u4e00\u4e9b\u5185\u5bb9\uff0c\u4f8b\u5982 createStore \u63d0\u4f9b\u7684 replaceReducer \u65b9\u6cd5\uff0c\u4ee5\u53ca createStore \u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u548c\u7b2c\u4e09\u4e2a\u53c2\u6570\u6ca1\u6709\u63d0\u53ca\uff0c\u7a0d\u5fae\u770b\u4e00\u4e0b\u4ee3\u7801\u5c31\u80fd\u61c2\uff0c\u6b64\u5904\u4e0d\u518d\u4e00\u4e00\u5c55\u5f00\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
Redux \u662f JavaScript \u72b6\u6001\u5bb9\u5668\uff0c\u63d0\u4f9b\u53ef\u9884\u6d4b\u5316\u7684\u72b6\u6001\u7ba1\u7406\u3002Redux \u9664\u4e86\u548c React \u4e00\u8d77\u7528 […]<\/p>\n","protected":false},"author":1898,"featured_media":160760,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[448],"class_list":["post-160758","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-redux"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/160758","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=160758"}],"version-history":[{"count":10,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/160758\/revisions"}],"predecessor-version":[{"id":160881,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/160758\/revisions\/160881"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/160760"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=160758"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=160758"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=160758"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}