2626
2727### ⊙ 源码分析
2828
29- ```
29+ ``` js
3030/**
3131 * 看起来逼格很高,实际运用其实是这样子的:
3232 * compose(f, g, h)(...arg) => f(g(h(...args)))
@@ -54,7 +54,7 @@ export default function compose(...funcs) {
5454
5555这里的关键点在于,` reduceRight ` 可传入初始值。由于 ` reduce / reduceRight ` 仅仅是方向的不同,因此下面用 ` reduce ` 说明即可:
5656
57- ```
57+ ``` js
5858var arr = [1 , 2 , 3 , 4 , 5 ]
5959
6060var re1 = arr .reduce (function (total , i ) {
@@ -71,7 +71,7 @@ console.log(re2) // 115
7171## § ; Redux API · [ createStore(reducer, [ initialState] )] [ createStore ]
7272### ⊙ 源码分析
7373
74- ```
74+ ``` js
7575import isPlainObject from ' lodash/isPlainObject'
7676import $$observable from ' symbol-observable'
7777
@@ -332,7 +332,7 @@ reducers/
332332 ├── todosReducer.js
333333```
334334
335- ```
335+ ``` js
336336/** 本代码块记为 code-9 **/
337337/* reducers/index.js */
338338import { combineReducers } from ' redux'
@@ -410,7 +410,7 @@ reducers/
410410 ├── todoListReducer.js
411411```
412412
413- ```
413+ ``` js
414414/* reducers/index.js */
415415import { combineReducers } from ' redux'
416416import counterReducer from ' ./counterReducer'
@@ -478,7 +478,7 @@ rootReducer(state, action) —→∑ ↗ optTimeReducer(optTime, action) ---
478478### ⊙ 源码分析
479479> 仅截取关键部分,毕竟有很大一部分都是类型检测警告
480480
481- ```
481+ ``` js
482482function combineReducers (reducers ) {
483483 var reducerKeys = Object .keys (reducers)
484484 var finalReducers = {}
@@ -518,7 +518,7 @@ function combineReducers(reducers) {
518518
519519### ⊙ 源码分析
520520
521- ```
521+ ``` js
522522/* 为 Action Creator 加装上自动 dispatch 技能 */
523523function bindActionCreator (actionCreator , dispatch ) {
524524 return (... args ) => dispatch (actionCreator (... args))
@@ -591,7 +591,7 @@ $('#btn').on('click', function() {
591591诸如统一的日志记录、引入 thunk 统一处理异步 Action Creator 等都属于中间件
592592下面是一个简单的打印动作前后 ` state ` 的中间件:
593593
594- ```
594+ ``` js
595595/* 装逼写法 */
596596const printStateMiddleware = ({ getState }) => next => action => {
597597 console .log (' state before dispatch' , getState ())
@@ -625,7 +625,7 @@ function printStateMiddleware(middlewareAPI) { // 记为【锚点-1】,中间
625625说白了,Store 增强器就是对生成的 ` store ` API 进行改造,这是它与中间件最大的区别(中间件不修改 ` store ` 的 API)
626626而改造 ` store ` 的 API 就要从它的缔造者 ` createStore ` 入手。例如,Redux 的 API ` applyMiddleware ` 就是一个 Store 增强器:
627627
628- ```
628+ ``` js
629629import compose from ' ./compose' // 这货的作用其实就是 compose(f, g, h)(action) => f(g(h(action)))
630630
631631/* 传入一坨中间件 */
@@ -668,7 +668,7 @@ export default function applyMiddleware(...middlewares) {
668668最终返回的虽然还是 ` store ` 的那四个 API,但其中的 ` dispatch ` 函数的功能被增强了,这就是所谓的 Store Enhancer
669669
670670### ⊙ 综合应用 ( [ 在线演示] [ jsbin ] )
671- ```
671+ ``` html
672672<!DOCTYPE html>
673673<html >
674674<head >
@@ -723,7 +723,7 @@ store.dispatch(dec());
723723
724724实际上,上面生成 ` store ` 的代码还可以更加优雅:
725725
726- ```
726+ ``` js
727727/** 本代码块记为 code-10 **/
728728var store = Redux .createStore (
729729 reducer,
@@ -735,7 +735,7 @@ var store = Redux.createStore(
735735
736736> 重温一下 ` createStore ` 完整的函数签名:` function createStore(reducer, preloadedState, enhancer) `
737737
738- ```
738+ ``` js
739739/** 本代码块记为 code-11 **/
740740import { createStore , applyMiddleware , compose } from ' redux'
741741
@@ -757,7 +757,7 @@ const store = createStore(
757757
758758在 ` createStore ` 的源码分析的开头部分,我省略了一些代码,现在奉上关键部分:
759759
760- ```
760+ ``` js
761761/** 本代码块记为 code-12 **/
762762if (typeof preloadedState === ' function' && typeof enhancer === ' undefined' ) {
763763 // 这里就是上面 code-10 的情况,只传入 reducer 和 Store Enhancer 这两个参数
0 commit comments