GeezerVison is showing github.com — Don't double-invoke effects in legacy roots (#20028) · react/react@02da938
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberClassComponent.new.js +28-5Lines changed: 28 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -30,7 +30,13 @@ import invariant from 'shared/invariant'; | 30 | 30 | import {REACT_CONTEXT_TYPE, REACT_PROVIDER_TYPE} from 'shared/ReactSymbols'; | 31 | 31 |
| 32 | 32 | import {resolveDefaultProps} from './ReactFiberLazyComponent.new'; | 33 | | -import {DebugTracingMode, StrictMode} from './ReactTypeOfMode'; | | 33 | +import { | | 34 | + BlockingMode, | | 35 | + ConcurrentMode, | | 36 | + DebugTracingMode, | | 37 | + NoMode, | | 38 | + StrictMode, | | 39 | +} from './ReactTypeOfMode'; | 34 | 40 |
| 35 | 41 | import { | 36 | 42 | enqueueUpdate, | @@ -891,7 +897,12 @@ function mountClassInstance( | 891 | 897 | } | 892 | 898 |
| 893 | 899 | if (typeof instance.componentDidMount === 'function') { | 894 | | - if (__DEV__ && enableDoubleInvokingEffects) { | | 900 | + if ( | | 901 | + __DEV__ && | | 902 | + enableDoubleInvokingEffects && | | 903 | + (workInProgress.mode & (BlockingMode | ConcurrentMode)) !== NoMode | | 904 | + ) { | | 905 | + // Never double-invoke effects for legacy roots. | 895 | 906 | workInProgress.flags |= MountLayoutDev | Update; | 896 | 907 | } else { | 897 | 908 | workInProgress.flags |= Update; | @@ -965,7 +976,11 @@ function resumeMountClassInstance( | 965 | 976 | // If an update was already in progress, we should schedule an Update | 966 | 977 | // effect even though we're bailing out, so that cWU/cDU are called. | 967 | 978 | if (typeof instance.componentDidMount === 'function') { | 968 | | - if (__DEV__ && enableDoubleInvokingEffects) { | | 979 | + if ( | | 980 | + __DEV__ && | | 981 | + enableDoubleInvokingEffects && | | 982 | + (workInProgress.mode & (BlockingMode | ConcurrentMode)) !== NoMode | | 983 | + ) { | 969 | 984 | workInProgress.flags |= MountLayoutDev | Update; | 970 | 985 | } else { | 971 | 986 | workInProgress.flags |= Update; | @@ -1012,7 +1027,11 @@ function resumeMountClassInstance( | 1012 | 1027 | } | 1013 | 1028 | } | 1014 | 1029 | if (typeof instance.componentDidMount === 'function') { | 1015 | | - if (__DEV__ && enableDoubleInvokingEffects) { | | 1030 | + if ( | | 1031 | + __DEV__ && | | 1032 | + enableDoubleInvokingEffects && | | 1033 | + (workInProgress.mode & (BlockingMode | ConcurrentMode)) !== NoMode | | 1034 | + ) { | 1016 | 1035 | workInProgress.flags |= MountLayoutDev | Update; | 1017 | 1036 | } else { | 1018 | 1037 | workInProgress.flags |= Update; | @@ -1022,7 +1041,11 @@ function resumeMountClassInstance( | 1022 | 1041 | // If an update was already in progress, we should schedule an Update | 1023 | 1042 | // effect even though we're bailing out, so that cWU/cDU are called. | 1024 | 1043 | if (typeof instance.componentDidMount === 'function') { | 1025 | | - if (__DEV__ && enableDoubleInvokingEffects) { | | 1044 | + if ( | | 1045 | + __DEV__ && | | 1046 | + enableDoubleInvokingEffects && | | 1047 | + (workInProgress.mode & (BlockingMode | ConcurrentMode)) !== NoMode | | 1048 | + ) { | 1026 | 1049 | workInProgress.flags |= MountLayoutDev | Update; | 1027 | 1050 | } else { | 1028 | 1051 | workInProgress.flags |= Update; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberCommitWork.new.js +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2024,6 +2024,8 @@ function commitPassiveMount( | 2024 | 2024 |
| 2025 | 2025 | function invokeLayoutEffectMountInDEV(fiber: Fiber): void { | 2026 | 2026 | if (__DEV__ && enableDoubleInvokingEffects) { | | 2027 | + // We don't need to re-check for legacy roots here. | | 2028 | + // This function will not be called within legacy roots. | 2027 | 2029 | switch (fiber.tag) { | 2028 | 2030 | case FunctionComponent: | 2029 | 2031 | case ForwardRef: | @@ -2057,6 +2059,8 @@ function invokeLayoutEffectMountInDEV(fiber: Fiber): void { | 2057 | 2059 |
| 2058 | 2060 | function invokePassiveEffectMountInDEV(fiber: Fiber): void { | 2059 | 2061 | if (__DEV__ && enableDoubleInvokingEffects) { | | 2062 | + // We don't need to re-check for legacy roots here. | | 2063 | + // This function will not be called within legacy roots. | 2060 | 2064 | switch (fiber.tag) { | 2061 | 2065 | case FunctionComponent: | 2062 | 2066 | case ForwardRef: | @@ -2081,6 +2085,8 @@ function invokePassiveEffectMountInDEV(fiber: Fiber): void { | 2081 | 2085 |
| 2082 | 2086 | function invokeLayoutEffectUnmountInDEV(fiber: Fiber): void { | 2083 | 2087 | if (__DEV__ && enableDoubleInvokingEffects) { | | 2088 | + // We don't need to re-check for legacy roots here. | | 2089 | + // This function will not be called within legacy roots. | 2084 | 2090 | switch (fiber.tag) { | 2085 | 2091 | case FunctionComponent: | 2086 | 2092 | case ForwardRef: | @@ -2113,6 +2119,8 @@ function invokeLayoutEffectUnmountInDEV(fiber: Fiber): void { | 2113 | 2119 |
| 2114 | 2120 | function invokePassiveEffectUnmountInDEV(fiber: Fiber): void { | 2115 | 2121 | if (__DEV__ && enableDoubleInvokingEffects) { | | 2122 | + // We don't need to re-check for legacy roots here. | | 2123 | + // This function will not be called within legacy roots. | 2116 | 2124 | switch (fiber.tag) { | 2117 | 2125 | case FunctionComponent: | 2118 | 2126 | case ForwardRef: | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberHooks.new.js +26-5Lines changed: 26 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,7 +29,12 @@ import { | 29 | 29 | enableDoubleInvokingEffects, | 30 | 30 | } from 'shared/ReactFeatureFlags'; | 31 | 31 |
| 32 | | -import {NoMode, BlockingMode, DebugTracingMode} from './ReactTypeOfMode'; | | 32 | +import { | | 33 | + NoMode, | | 34 | + BlockingMode, | | 35 | + ConcurrentMode, | | 36 | + DebugTracingMode, | | 37 | +} from './ReactTypeOfMode'; | 33 | 38 | import { | 34 | 39 | NoLane, | 35 | 40 | NoLanes, | @@ -485,7 +490,11 @@ export function bailoutHooks( | 485 | 490 | lanes: Lanes, | 486 | 491 | ) { | 487 | 492 | workInProgress.updateQueue = current.updateQueue; | 488 | | - if (__DEV__ && enableDoubleInvokingEffects) { | | 493 | + if ( | | 494 | + __DEV__ && | | 495 | + enableDoubleInvokingEffects && | | 496 | + (workInProgress.mode & (BlockingMode | ConcurrentMode)) !== NoMode | | 497 | + ) { | 489 | 498 | workInProgress.flags &= ~( | 490 | 499 | MountPassiveDevEffect | | 491 | 500 | PassiveEffect | | @@ -1253,7 +1262,11 @@ function mountEffect( | 1253 | 1262 | } | 1254 | 1263 | } | 1255 | 1264 |
| 1256 | | - if (__DEV__ && enableDoubleInvokingEffects) { | | 1265 | + if ( | | 1266 | + __DEV__ && | | 1267 | + enableDoubleInvokingEffects && | | 1268 | + (currentlyRenderingFiber.mode & (BlockingMode | ConcurrentMode)) !== NoMode | | 1269 | + ) { | 1257 | 1270 | return mountEffectImpl( | 1258 | 1271 | MountPassiveDevEffect | PassiveEffect | PassiveStaticEffect, | 1259 | 1272 | HookPassive, | @@ -1287,7 +1300,11 @@ function mountLayoutEffect( | 1287 | 1300 | create: () => (() => void) | void, | 1288 | 1301 | deps: Array<mixed> | void | null, | 1289 | 1302 | ): void { | 1290 | | - if (__DEV__ && enableDoubleInvokingEffects) { | | 1303 | + if ( | | 1304 | + __DEV__ && | | 1305 | + enableDoubleInvokingEffects && | | 1306 | + (currentlyRenderingFiber.mode & (BlockingMode | ConcurrentMode)) !== NoMode | | 1307 | + ) { | 1291 | 1308 | return mountEffectImpl( | 1292 | 1309 | MountLayoutDevEffect | UpdateEffect, | 1293 | 1310 | HookLayout, | @@ -1355,7 +1372,11 @@ function mountImperativeHandle<T>( | 1355 | 1372 | const effectDeps = | 1356 | 1373 | deps !== null && deps !== undefined ? deps.concat([ref]) : null; | 1357 | 1374 |
| 1358 | | - if (__DEV__ && enableDoubleInvokingEffects) { | | 1375 | + if ( | | 1376 | + __DEV__ && | | 1377 | + enableDoubleInvokingEffects && | | 1378 | + (currentlyRenderingFiber.mode & (BlockingMode | ConcurrentMode)) !== NoMode | | 1379 | + ) { | 1359 | 1380 | return mountEffectImpl( | 1360 | 1381 | MountLayoutDevEffect | UpdateEffect, | 1361 | 1382 | HookLayout, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberWorkLoop.new.js +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2874,6 +2874,11 @@ function commitDoubleInvokeEffectsInDEV( | 2874 | 2874 | hasPassiveEffects: boolean, | 2875 | 2875 | ) { | 2876 | 2876 | if (__DEV__ && enableDoubleInvokingEffects) { | | 2877 | + // Never double-invoke effects for legacy roots. | | 2878 | + if ((fiber.mode & (BlockingMode | ConcurrentMode)) === NoMode) { | | 2879 | + return; | | 2880 | + } | | 2881 | + | 2877 | 2882 | setCurrentDebugFiberInDEV(fiber); | 2878 | 2883 | invokeEffectsInDev(fiber, MountLayoutDev, invokeLayoutEffectUnmountInDEV); | 2879 | 2884 | if (hasPassiveEffects) { | @@ -2898,6 +2903,8 @@ function invokeEffectsInDev( | 2898 | 2903 | invokeEffectFn: (fiber: Fiber) => void, | 2899 | 2904 | ): void { | 2900 | 2905 | if (__DEV__ && enableDoubleInvokingEffects) { | | 2906 | + // We don't need to re-check for legacy roots here. | | 2907 | + // This function will not be called within legacy roots. | 2901 | 2908 | let fiber = firstChild; | 2902 | 2909 | while (fiber !== null) { | 2903 | 2910 | if (fiber.child !== null) { | |
|