@@ -44,9 +44,14 @@ var reducer_1 = function (state = {}, action) {
44
44
45
45
// It become quite evident that a single reducer function cannot hold all our
46
46
// application's actions handling (well it could hold it, but it wouldn't be very maintainable...).
47
+ //
48
+ // 很明显,一个单独的reducer函数是不足以处理我们所有的actions(当然他可以全部容纳进来,但是却非常难以维护)
49
+
47
50
48
51
// Luckily for us, Redux doesn't care if we have one reducer or a dozen and it will even help us to
49
52
// combine them if we have many!
53
+ //
54
+ // 幸运的是,Redux并不关心我们有几个reducer.甚至当我们有多个reducer时,他会帮助我们将这些reducer合并到一起
50
55
51
56
// Let's declare 2 reducers
52
57
@@ -71,27 +76,41 @@ var itemsReducer = function (state = [], action) {
71
76
72
77
// With this new multiple reducer approach, we will end up having each reducer to only handle
73
78
// a slice of our application state.
79
+ //
80
+ // 当发现有多个reducer时。我们最终也会使每个reducer处理应用状态的一部分。
74
81
75
82
// But as we already know, just one single reducer function is expected by createStore.
83
+ // 但是我们了解到,createStore函数只接受一个reducer函数
76
84
77
85
// So how do we combine our reducers? and how do we tell Redux that each reducer will only handle
78
86
// a slice of our state?
87
+ // 那么我们要怎么组合我们的reducers呢?并且我们要怎么告知Redux每个reducer要处理哪一部分的state?
88
+ //
79
89
// It's fairly simple. We use Redux combineReducers function helper. combineReducers take a hash and
80
90
// return a function that when invoked, will call all our reducers, retrieve the state new slice and
81
91
// reunite them in a state object (a simple hash {}) that Redux is holding.
82
92
// Long story short, here is how you create a Redux instance with multiple reducers:
93
+ // 这非常简单。我们可以使用Redux的combineReducer辅助函数。combineReducer接受一个hash对象,当调用后会
94
+ // 返回一个函数。将会调用我们所有的reducers,获取新的一段state。最终将他们都返回并合并为一个新的object
95
+ // (一个简单的hash{})让Redux继续保持。
96
+ // 总而言之,这就是我们如何通过多个reducers来创建Redux实体。
97
+
83
98
84
99
import { createStore , combineReducers } from 'redux'
85
100
86
101
var reducer = combineReducers ( {
87
102
user : userReducer ,
88
103
items : itemsReducer
89
104
} )
105
+
106
+ console . log ( "Init by combineReducer" ) ;
107
+
90
108
// Output:
91
109
// userReducer was called with state {} and action { type: '@@redux/INIT' }
92
110
// userReducer was called with state {} and action { type: 'a.2.e.i.j.9.e.j.y.v.i' }
93
111
// itemsReducer was called with state [] and action { type: '@@redux/INIT' }
94
112
// itemsReducer was called with state [] and action { type: 'i.l.j.c.a.4.z.3.3.d.i' }
113
+ //
95
114
var store_0 = createStore ( reducer )
96
115
// Output:
97
116
// userReducer was called with state {} and action { type: '@@redux/INIT' }
@@ -102,6 +121,10 @@ var store_0 = createStore(reducer)
102
121
// to assure that a reducer will always return a state != 'undefined'.
103
122
// Please note also that the first invocation of init actions in combineReducers share the same purpose
104
123
// as random actions (to do a sanity check).
124
+ // 通过output我们了解到,在初始化时,每个reducer都正确的调用了init action @@redux/INIT
125
+ // 但是。如果另一个action到达后怎么去接收呢?在combineReducers中合理的进行了检查,以确保reducer总是返回一个不等于
126
+ // 'undefined'的state.
127
+ // 也请注意,当第一次调用CombineReducer时,除了调用init action,也一起调用了一个随机的action来完成合理性检查的目的。
105
128
106
129
console . log ( 'store_0 state after initialization:' , store_0 . getState ( ) )
107
130
// Output:
@@ -113,8 +136,17 @@ console.log('store_0 state after initialization:', store_0.getState())
113
136
// user: {}, // {} is the slice returned by our userReducer
114
137
// items: [] // [] is the slice returned by our itemsReducer
115
138
// }
139
+ //
140
+ // 有趣的是,Redux确实是支持我们对reducer分段的,而且最终state合并了userReducer 与 itemsReducer的分段,并返回了一个完整的state
141
+ // {
142
+ // user: {}, // {} is the slice returned by our userReducer
143
+ // items: [] // [] is the slice returned by our itemsReducer
144
+ // }
116
145
117
146
// We have by now a good idea of how reducers will work. It would be nice to have some
118
147
// actions being dispatched and see the impact on our Redux state.
148
+ //
149
+ // 我们现在对reducers的运行有了一个深入的了解,下面来看一下 当同一个actions被派发时,reducer是如何对
150
+ // Redux产生影响的。
119
151
120
152
// Go to next tutorial: dispatch-action.js
0 commit comments