@@ -35,6 +35,7 @@ import {
3535 enableFundamentalAPI ,
3636 enableSuspenseCallback ,
3737 enableScopeAPI ,
38+ enableDoubleInvokingEffects ,
3839} from 'shared/ReactFeatureFlags' ;
3940import {
4041 FunctionComponent ,
@@ -313,7 +314,8 @@ function commitBeforeMutationLifeCycles(
313314 ) ;
314315}
315316
316- function commitHookEffectListUnmount ( tag : number , finishedWork : Fiber ) {
317+ function commitHookLayoutEffectListUnmount ( finishedWork : Fiber ) {
318+ const tag = HookLayout | HookHasEffect ;
317319 const updateQueue : FunctionComponentUpdateQueue | null = ( finishedWork . updateQueue : any ) ;
318320 const lastEffect = updateQueue !== null ? updateQueue . lastEffect : null ;
319321 if ( lastEffect !== null ) {
@@ -325,15 +327,29 @@ function commitHookEffectListUnmount(tag: number, finishedWork: Fiber) {
325327 const destroy = effect . destroy ;
326328 effect . destroy = undefined ;
327329 if ( destroy !== undefined ) {
328- destroy ( ) ;
330+ if (
331+ enableProfilerTimer &&
332+ enableProfilerCommitHooks &&
333+ finishedWork . mode & ProfileMode
334+ ) {
335+ try {
336+ startLayoutEffectTimer ( ) ;
337+ destroy ( ) ;
338+ } finally {
339+ recordLayoutEffectDuration ( finishedWork ) ;
340+ }
341+ } else {
342+ destroy ( ) ;
343+ }
329344 }
330345 }
331346 effect = effect . next ;
332347 } while ( effect !== firstEffect ) ;
333348 }
334349}
335350
336- function commitHookEffectListMount ( tag : number , finishedWork : Fiber ) {
351+ function commitHookLayoutEffectListMount ( finishedWork : Fiber ) {
352+ const tag = HookLayout | HookHasEffect ;
337353 const updateQueue : FunctionComponentUpdateQueue | null = ( finishedWork . updateQueue : any ) ;
338354 const lastEffect = updateQueue !== null ? updateQueue . lastEffect : null ;
339355 if ( lastEffect !== null ) {
@@ -343,8 +359,20 @@ function commitHookEffectListMount(tag: number, finishedWork: Fiber) {
343359 if ( ( effect . tag & tag ) === tag ) {
344360 // Mount
345361 const create = effect . create ;
346- effect . destroy = create ( ) ;
347-
362+ if (
363+ enableProfilerTimer &&
364+ enableProfilerCommitHooks &&
365+ finishedWork . mode & ProfileMode
366+ ) {
367+ try {
368+ startLayoutEffectTimer ( ) ;
369+ effect . destroy = create ( ) ;
370+ } finally {
371+ recordLayoutEffectDuration ( finishedWork ) ;
372+ }
373+ } else {
374+ effect . destroy = create ( ) ;
375+ }
348376 if ( __DEV__ ) {
349377 const destroy = effect . destroy ;
350378 if ( destroy !== undefined && typeof destroy !== 'function' ) {
@@ -376,6 +404,14 @@ function commitHookEffectListMount(tag: number, finishedWork: Fiber) {
376404 addendum ,
377405 ) ;
378406 }
407+
408+ if ( enableDoubleInvokingEffects ) {
409+ effect . destroy = undefined ;
410+ if ( typeof destroy === 'function' ) {
411+ destroy ( ) ;
412+ }
413+ effect . destroy = create ( ) ;
414+ }
379415 }
380416 }
381417 effect = effect . next ;
@@ -453,20 +489,7 @@ function commitLifeCycles(
453489 // This is done to prevent sibling component effects from interfering with each other,
454490 // e.g. a destroy function in one component should never override a ref set
455491 // by a create function in another component during the same commit.
456- if (
457- enableProfilerTimer &&
458- enableProfilerCommitHooks &&
459- finishedWork . mode & ProfileMode
460- ) {
461- try {
462- startLayoutEffectTimer ( ) ;
463- commitHookEffectListMount ( HookLayout | HookHasEffect , finishedWork ) ;
464- } finally {
465- recordLayoutEffectDuration ( finishedWork ) ;
466- }
467- } else {
468- commitHookEffectListMount ( HookLayout | HookHasEffect , finishedWork ) ;
469- }
492+ commitHookLayoutEffectListMount ( finishedWork ) ;
470493
471494 if ( ( finishedWork . subtreeTag & PassiveSubtreeTag ) !== NoSubtreeTag ) {
472495 schedulePassiveEffectCallback ( ) ;
@@ -521,6 +544,16 @@ function commitLifeCycles(
521544 } else {
522545 instance . componentDidMount ( ) ;
523546 }
547+
548+ if ( __DEV__ ) {
549+ if ( enableDoubleInvokingEffects ) {
550+ // if there is no unmount function we don't have to double invoke
551+ if ( typeof instance . componentWillUnmount === 'function' ) {
552+ safelyCallComponentWillUnmount ( finishedWork , instance ) ;
553+ }
554+ instance . componentDidMount ( ) ;
555+ }
556+ }
524557 } else {
525558 const prevProps =
526559 finishedWork . elementType === finishedWork . type
@@ -1428,23 +1461,7 @@ function commitWork(current: Fiber | null, finishedWork: Fiber): void {
14281461 // This prevents sibling component effects from interfering with each other,
14291462 // e.g. a destroy function in one component should never override a ref set
14301463 // by a create function in another component during the same commit.
1431- if (
1432- enableProfilerTimer &&
1433- enableProfilerCommitHooks &&
1434- finishedWork . mode & ProfileMode
1435- ) {
1436- try {
1437- startLayoutEffectTimer ( ) ;
1438- commitHookEffectListUnmount (
1439- HookLayout | HookHasEffect ,
1440- finishedWork ,
1441- ) ;
1442- } finally {
1443- recordLayoutEffectDuration ( finishedWork ) ;
1444- }
1445- } else {
1446- commitHookEffectListUnmount ( HookLayout | HookHasEffect , finishedWork ) ;
1447- }
1464+ commitHookLayoutEffectListUnmount ( finishedWork ) ;
14481465 return ;
14491466 }
14501467 case Profiler : {
@@ -1491,20 +1508,7 @@ function commitWork(current: Fiber | null, finishedWork: Fiber): void {
14911508 // This prevents sibling component effects from interfering with each other,
14921509 // e.g. a destroy function in one component should never override a ref set
14931510 // by a create function in another component during the same commit.
1494- if (
1495- enableProfilerTimer &&
1496- enableProfilerCommitHooks &&
1497- finishedWork . mode & ProfileMode
1498- ) {
1499- try {
1500- startLayoutEffectTimer ( ) ;
1501- commitHookEffectListUnmount ( HookLayout | HookHasEffect , finishedWork ) ;
1502- } finally {
1503- recordLayoutEffectDuration ( finishedWork ) ;
1504- }
1505- } else {
1506- commitHookEffectListUnmount ( HookLayout | HookHasEffect , finishedWork ) ;
1507- }
1511+ commitHookLayoutEffectListUnmount ( finishedWork ) ;
15081512 return ;
15091513 }
15101514 case ClassComponent : {
0 commit comments