@@ -176,5 +176,83 @@ Component.prototype.setState = function (partialState, callback) {
176
176
``` ts
177
177
enqueueSetState (inst , payload , callback ) {}
178
178
179
+ ```
180
+
181
+ 第七章
182
+
183
+ mount
184
+
185
+ ``` ts
186
+ function mountMemo<T >(nextCreate : () => T , deps : Array <mixed > | void | null ): T {
187
+ // 创建并返回当前hook
188
+ const hook = mountWorkInProgressHook ()
189
+ const nextDeps = deps === undefined ? null : deps
190
+ const nextValue = nextCreate ()
191
+ hook .memoizedState = [nextValue , nextDeps ]
192
+ return nextValue
193
+ }
194
+
195
+ function mountCallback<T >(callback : T , deps : Array <mixed > | void | null ): T {
196
+ const hook = mountWorkInProgressHook ()
197
+ const nextDeps = deps === undefined ? null : deps
198
+ hook .memoizedState = [callback , nextDeps ];
199
+ return callback
200
+ }
201
+
202
+ ```
179
203
204
+ update
205
+
206
+ ``` ts
207
+ function updateMemo<T >(
208
+ nextCreate : () => T ,
209
+ deps : Array <mixed > | void | null ,
210
+ ): T {
211
+ // 返回当前hook
212
+ const hook = updateWorkInProgressHook ();
213
+ const nextDeps = deps === undefined ? null : deps ;
214
+ const prevState = hook .memoizedState ;
215
+
216
+ if (prevState !== null ) {
217
+ if (nextDeps !== null ) {
218
+ const prevDeps: Array <mixed > | null = prevState [1 ];
219
+ // 判断update前后value是否变化
220
+ if (areHookInputsEqual (nextDeps , prevDeps )) {
221
+ // 未变化
222
+ return prevState [0 ];
223
+ }
224
+ }
225
+ }
226
+ // 变化,重新计算value
227
+ const nextValue = nextCreate ();
228
+ hook .memoizedState = [nextValue , nextDeps ];
229
+ return nextValue ;
230
+ }
231
+
232
+ function updateCallback<T >(callback : T , deps : Array <mixed > | void | null ): T {
233
+ // 返回当前hook
234
+ const hook = updateWorkInProgressHook ();
235
+ const nextDeps = deps === undefined ? null : deps ;
236
+ const prevState = hook .memoizedState ;
237
+
238
+ if (prevState !== null ) {
239
+ if (nextDeps !== null ) {
240
+ const prevDeps: Array <mixed > | null = prevState [1 ];
241
+ // 判断update前后value是否变化
242
+ if (areHookInputsEqual (nextDeps , prevDeps )) {
243
+ // 未变化
244
+ return prevState [0 ];
245
+ }
246
+ }
247
+ }
248
+
249
+ // 变化,将新的callback作为value
250
+ hook .memoizedState = [callback , nextDeps ];
251
+ return callback ;
252
+ }
180
253
```
254
+
255
+ useMemo和useCallback的差别就是:useMemo返回的上次的值,而后者是返回callback
256
+
257
+ 第八章
258
+ Concurrent 模式是一组 React 的新功能,可帮助应用保持响应,并根据用户的设备性能和网速进行适当的调整。
0 commit comments