@@ -169,61 +169,52 @@ export class Component extends BaseComponent {
169
169
}
170
170
171
171
forceUpdate ( cb ) {
172
- if ( ! ( this instanceof HocComponent ) && ! this . getWxInst ( ) ) {
173
- console . warn ( 'the component has unmount, should not invoke forceUpdate!!' )
174
- return
175
- }
176
-
177
- this . componentWillUpdate && this . componentWillUpdate ( this . props , this . state )
178
- this . UNSAFE_componentWillUpdate && this . UNSAFE_componentWillUpdate ( this . props , this . state )
179
-
180
- const subVnode = this . render ( )
181
- this . _or = this . _r
182
- this . _r = { }
183
- this . _c = [ ]
184
- this . __eventHanderMap = { }
185
-
186
- const parentContext = this . _parentContext
187
- const context = getCurrentContext ( this , parentContext )
188
-
189
- render ( subVnode , this , context , this . _r , this . _or , '_r' )
190
-
191
- this . flushDataToWx ( subVnode , cb )
172
+ this . updateInner ( { } , cb , true )
192
173
}
193
174
194
175
setState ( newState , cb ) {
195
- // 还未渲染完成调用setState,通常发生在willMount等处,此时把更新入队,在渲染结束的时候会统一检查一下这种情况的更新
176
+ this . updateInner ( newState , cb , false )
177
+ }
178
+
179
+ updateInner ( newState , cb , isForce ) {
180
+ // 还未渲染完成调用setState/forceUpdate,通常发生在willMount等处,此时把更新入队,在渲染结束的时候会统一检查一下这种情况的更新
196
181
if ( ! ( this instanceof HocComponent ) && this . firstRender !== FR_DONE ) {
197
182
this . updateQueue . push ( newState )
198
183
cb && this . updateQueueCB . push ( cb )
184
+ if ( isForce ) {
185
+ this . isForceUpdate = isForce
186
+ }
199
187
200
188
return
201
189
}
202
190
203
191
// 没有找到对应wxInst
204
192
if ( ! ( this instanceof HocComponent ) && ! this . getWxInst ( ) ) {
205
- console . warn ( ' the component has unmount, should not invoke setState!!' )
193
+ console . warn ( ` the component has unmount, should not invoke ${ isForce ? 'forceUpdate' : ' setState' } !!` )
206
194
return
207
195
}
208
196
209
-
210
197
if ( reactUpdate . inRe ( ) ) {
211
198
this . updateQueue . push ( newState )
212
199
if ( cb ) {
213
200
this . updateQueueCB . push ( cb )
214
201
}
202
+ if ( isForce ) {
203
+ this . isForceUpdate = isForce
204
+ }
215
205
reactUpdate . addUpdateInst ( this )
216
206
return
217
207
}
218
208
219
-
220
209
const nextState = {
221
210
...this . state ,
222
211
...newState
223
212
}
224
213
225
214
let shouldUpdate
226
- if ( this . shouldComponentUpdate ) {
215
+ if ( isForce ) {
216
+ shouldUpdate = true
217
+ } else if ( this . shouldComponentUpdate ) {
227
218
shouldUpdate = this . shouldComponentUpdate ( this . props , nextState )
228
219
} else {
229
220
shouldUpdate = true
@@ -232,14 +223,12 @@ export class Component extends BaseComponent {
232
223
shouldUpdate && this . componentWillUpdate && this . componentWillUpdate ( this . props , nextState )
233
224
shouldUpdate && this . UNSAFE_componentWillUpdate && this . UNSAFE_componentWillUpdate ( this . props , nextState )
234
225
235
-
236
226
this . state = nextState
237
227
238
228
if ( ! shouldUpdate ) {
239
229
return // do nothing
240
230
}
241
231
242
-
243
232
const subVnode = this . render ( )
244
233
245
234
/// 重置实例字段:_or 旧的渲染数据, _r 渲染数据, _c 所以孩子实例 , __eventHanderMap 方法回调map
@@ -255,25 +244,11 @@ export class Component extends BaseComponent {
255
244
256
245
render ( subVnode , this , context , this . _r , this . _or , '_r' )
257
246
258
- let finalCb = null
259
- // 合并setState 回调
260
- if ( this . updateQueueCB . length > 0 ) {
261
- const cbQueue = this . updateQueueCB
262
- finalCb = ( ) => {
263
- for ( let i = 0 ; i < cbQueue . length ; i ++ ) {
264
- cbQueue [ i ] . call ( this )
265
- }
266
- }
267
- this . updateQueueCB = [ ]
268
- } else {
269
- finalCb = cb
270
- }
271
-
272
- this . flushDataToWx ( subVnode , finalCb )
247
+ this . flushDataToWx ( subVnode , cb )
273
248
}
274
249
275
250
/**
276
- * render过程结束之后,各组件_r 数据已经生成完毕 ,执行updateUI递归的把数据刷新到小程序。
251
+ * render过程结束之后,各组件渲染数据已经生成完毕 ,执行updateUI递归的把数据刷新到小程序。
277
252
* 此外:由于微信小程序自定义组件会生成一个节点,需要把内部最外层样式上报到这个节点
278
253
*/
279
254
flushDataToWx ( subVnode , finalCb ) {
0 commit comments