GeezerVison is showing github.com — Double Invoke Effects in __DEV__ (in old reconciler fork) (#20415) · react/react@56a632a
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.old.js +39-6Lines changed: 39 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,13 +12,14 @@ import type {Lanes} from './ReactFiberLane.old'; | 12 | 12 | import type {UpdateQueue} from './ReactUpdateQueue.old'; | 13 | 13 |
| 14 | 14 | import * as React from 'react'; | 15 | | -import {Update, Snapshot} from './ReactFiberFlags'; | | 15 | +import {Update, Snapshot, MountLayoutDev} from './ReactFiberFlags'; | 16 | 16 | import { | 17 | 17 | debugRenderPhaseSideEffectsForStrictMode, | 18 | 18 | disableLegacyContext, | 19 | 19 | enableDebugTracing, | 20 | 20 | enableSchedulingProfiler, | 21 | 21 | warnAboutDeprecatedLifecycles, | | 22 | + enableDoubleInvokingEffects, | 22 | 23 | } from 'shared/ReactFeatureFlags'; | 23 | 24 | import ReactStrictModeWarnings from './ReactStrictModeWarnings.old'; | 24 | 25 | import {isMounted} from './ReactFiberTreeReflection'; | @@ -29,7 +30,7 @@ import invariant from 'shared/invariant'; | 29 | 30 | import {REACT_CONTEXT_TYPE, REACT_PROVIDER_TYPE} from 'shared/ReactSymbols'; | 30 | 31 |
| 31 | 32 | import {resolveDefaultProps} from './ReactFiberLazyComponent.old'; | 32 | | -import {DebugTracingMode, StrictMode} from './ReactTypeOfMode'; | | 33 | +import {DebugTracingMode, NoMode, StrictMode} from './ReactTypeOfMode'; | 33 | 34 |
| 34 | 35 | import { | 35 | 36 | enqueueUpdate, | @@ -890,7 +891,15 @@ function mountClassInstance( | 890 | 891 | } | 891 | 892 |
| 892 | 893 | if (typeof instance.componentDidMount === 'function') { | 893 | | - workInProgress.flags |= Update; | | 894 | + if ( | | 895 | + __DEV__ && | | 896 | + enableDoubleInvokingEffects && | | 897 | + (workInProgress.mode & StrictMode) !== NoMode | | 898 | + ) { | | 899 | + workInProgress.flags |= MountLayoutDev | Update; | | 900 | + } else { | | 901 | + workInProgress.flags |= Update; | | 902 | + } | 894 | 903 | } | 895 | 904 | } | 896 | 905 |
| @@ -960,7 +969,15 @@ function resumeMountClassInstance( | 960 | 969 | // If an update was already in progress, we should schedule an Update | 961 | 970 | // effect even though we're bailing out, so that cWU/cDU are called. | 962 | 971 | if (typeof instance.componentDidMount === 'function') { | 963 | | - workInProgress.flags |= Update; | | 972 | + if ( | | 973 | + __DEV__ && | | 974 | + enableDoubleInvokingEffects && | | 975 | + (workInProgress.mode & StrictMode) !== NoMode | | 976 | + ) { | | 977 | + workInProgress.flags |= MountLayoutDev | Update; | | 978 | + } else { | | 979 | + workInProgress.flags |= Update; | | 980 | + } | 964 | 981 | } | 965 | 982 | return false; | 966 | 983 | } | @@ -1003,13 +1020,29 @@ function resumeMountClassInstance( | 1003 | 1020 | } | 1004 | 1021 | } | 1005 | 1022 | if (typeof instance.componentDidMount === 'function') { | 1006 | | - workInProgress.flags |= Update; | | 1023 | + if ( | | 1024 | + __DEV__ && | | 1025 | + enableDoubleInvokingEffects && | | 1026 | + (workInProgress.mode & StrictMode) !== NoMode | | 1027 | + ) { | | 1028 | + workInProgress.flags |= MountLayoutDev | Update; | | 1029 | + } else { | | 1030 | + workInProgress.flags |= Update; | | 1031 | + } | 1007 | 1032 | } | 1008 | 1033 | } else { | 1009 | 1034 | // If an update was already in progress, we should schedule an Update | 1010 | 1035 | // effect even though we're bailing out, so that cWU/cDU are called. | 1011 | 1036 | if (typeof instance.componentDidMount === 'function') { | 1012 | | - workInProgress.flags |= Update; | | 1037 | + if ( | | 1038 | + __DEV__ && | | 1039 | + enableDoubleInvokingEffects && | | 1040 | + (workInProgress.mode & StrictMode) !== NoMode | | 1041 | + ) { | | 1042 | + workInProgress.flags |= MountLayoutDev | Update; | | 1043 | + } else { | | 1044 | + workInProgress.flags |= Update; | | 1045 | + } | 1013 | 1046 | } | 1014 | 1047 |
| 1015 | 1048 | // If shouldComponentUpdate returned false, we should still update the | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberCommitWork.old.js +115Lines changed: 115 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,6 +35,7 @@ import { | 35 | 35 | enableFundamentalAPI, | 36 | 36 | enableSuspenseCallback, | 37 | 37 | enableScopeAPI, | | 38 | + enableDoubleInvokingEffects, | 38 | 39 | } from 'shared/ReactFeatureFlags'; | 39 | 40 | import { | 40 | 41 | FunctionComponent, | @@ -1798,6 +1799,116 @@ function commitResetTextContent(current: Fiber) { | 1798 | 1799 | resetTextContent(current.stateNode); | 1799 | 1800 | } | 1800 | 1801 |
| | 1802 | +function invokeLayoutEffectMountInDEV(fiber: Fiber): void { | | 1803 | + if (__DEV__ && enableDoubleInvokingEffects) { | | 1804 | + switch (fiber.tag) { | | 1805 | + case FunctionComponent: | | 1806 | + case ForwardRef: | | 1807 | + case SimpleMemoComponent: { | | 1808 | + invokeGuardedCallback( | | 1809 | + null, | | 1810 | + commitHookEffectListMount, | | 1811 | + null, | | 1812 | + HookLayout | HookHasEffect, | | 1813 | + fiber, | | 1814 | + ); | | 1815 | + if (hasCaughtError()) { | | 1816 | + const mountError = clearCaughtError(); | | 1817 | + captureCommitPhaseError(fiber, mountError); | | 1818 | + } | | 1819 | + break; | | 1820 | + } | | 1821 | + case ClassComponent: { | | 1822 | + const instance = fiber.stateNode; | | 1823 | + invokeGuardedCallback(null, instance.componentDidMount, instance); | | 1824 | + if (hasCaughtError()) { | | 1825 | + const mountError = clearCaughtError(); | | 1826 | + captureCommitPhaseError(fiber, mountError); | | 1827 | + } | | 1828 | + break; | | 1829 | + } | | 1830 | + } | | 1831 | + } | | 1832 | +} | | 1833 | + | | 1834 | +function invokePassiveEffectMountInDEV(fiber: Fiber): void { | | 1835 | + if (__DEV__ && enableDoubleInvokingEffects) { | | 1836 | + switch (fiber.tag) { | | 1837 | + case FunctionComponent: | | 1838 | + case ForwardRef: | | 1839 | + case SimpleMemoComponent: { | | 1840 | + invokeGuardedCallback( | | 1841 | + null, | | 1842 | + commitHookEffectListMount, | | 1843 | + null, | | 1844 | + HookPassive | HookHasEffect, | | 1845 | + fiber, | | 1846 | + ); | | 1847 | + if (hasCaughtError()) { | | 1848 | + const mountError = clearCaughtError(); | | 1849 | + captureCommitPhaseError(fiber, mountError); | | 1850 | + } | | 1851 | + break; | | 1852 | + } | | 1853 | + } | | 1854 | + } | | 1855 | +} | | 1856 | + | | 1857 | +function invokeLayoutEffectUnmountInDEV(fiber: Fiber): void { | | 1858 | + if (__DEV__ && enableDoubleInvokingEffects) { | | 1859 | + switch (fiber.tag) { | | 1860 | + case FunctionComponent: | | 1861 | + case ForwardRef: | | 1862 | + case SimpleMemoComponent: { | | 1863 | + invokeGuardedCallback( | | 1864 | + null, | | 1865 | + commitHookEffectListUnmount, | | 1866 | + null, | | 1867 | + HookLayout | HookHasEffect, | | 1868 | + fiber, | | 1869 | + fiber.return, | | 1870 | + ); | | 1871 | + if (hasCaughtError()) { | | 1872 | + const unmountError = clearCaughtError(); | | 1873 | + captureCommitPhaseError(fiber, unmountError); | | 1874 | + } | | 1875 | + break; | | 1876 | + } | | 1877 | + case ClassComponent: { | | 1878 | + const instance = fiber.stateNode; | | 1879 | + if (typeof instance.componentWillUnmount === 'function') { | | 1880 | + safelyCallComponentWillUnmount(fiber, instance); | | 1881 | + } | | 1882 | + break; | | 1883 | + } | | 1884 | + } | | 1885 | + } | | 1886 | +} | | 1887 | + | | 1888 | +function invokePassiveEffectUnmountInDEV(fiber: Fiber): void { | | 1889 | + if (__DEV__ && enableDoubleInvokingEffects) { | | 1890 | + switch (fiber.tag) { | | 1891 | + case FunctionComponent: | | 1892 | + case ForwardRef: | | 1893 | + case SimpleMemoComponent: { | | 1894 | + invokeGuardedCallback( | | 1895 | + null, | | 1896 | + commitHookEffectListUnmount, | | 1897 | + null, | | 1898 | + HookPassive | HookHasEffect, | | 1899 | + fiber, | | 1900 | + fiber.return, | | 1901 | + ); | | 1902 | + if (hasCaughtError()) { | | 1903 | + const unmountError = clearCaughtError(); | | 1904 | + captureCommitPhaseError(fiber, unmountError); | | 1905 | + } | | 1906 | + break; | | 1907 | + } | | 1908 | + } | | 1909 | + } | | 1910 | +} | | 1911 | + | 1801 | 1912 | export { | 1802 | 1913 | commitBeforeMutationLifeCycles, | 1803 | 1914 | commitResetTextContent, | @@ -1807,4 +1918,8 @@ export { | 1807 | 1918 | commitLifeCycles, | 1808 | 1919 | commitAttachRef, | 1809 | 1920 | commitDetachRef, | | 1921 | + invokeLayoutEffectMountInDEV, | | 1922 | + invokeLayoutEffectUnmountInDEV, | | 1923 | + invokePassiveEffectMountInDEV, | | 1924 | + invokePassiveEffectUnmountInDEV, | 1810 | 1925 | }; |
Collapse file Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiberHooks.old.js +87-16Lines changed: 87 additions & 16 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,9 +28,15 @@ import { | 28 | 28 | enableCache, | 29 | 29 | decoupleUpdatePriorityFromScheduler, | 30 | 30 | enableUseRefAccessWarning, | | 31 | + enableDoubleInvokingEffects, | 31 | 32 | } from 'shared/ReactFeatureFlags'; | 32 | 33 |
| 33 | | -import {NoMode, BlockingMode, DebugTracingMode} from './ReactTypeOfMode'; | | 34 | +import { | | 35 | + NoMode, | | 36 | + BlockingMode, | | 37 | + DebugTracingMode, | | 38 | + StrictMode, | | 39 | +} from './ReactTypeOfMode'; | 34 | 40 | import { | 35 | 41 | NoLane, | 36 | 42 | NoLanes, | @@ -49,6 +55,8 @@ import {readContext} from './ReactFiberNewContext.old'; | 49 | 55 | import { | 50 | 56 | Update as UpdateEffect, | 51 | 57 | Passive as PassiveEffect, | | 58 | + MountLayoutDev as MountLayoutDevEffect, | | 59 | + MountPassiveDev as MountPassiveDevEffect, | 52 | 60 | } from './ReactFiberFlags'; | 53 | 61 | import { | 54 | 62 | HasEffect as HookHasEffect, | @@ -467,7 +475,20 @@ export function bailoutHooks( | 467 | 475 | lanes: Lanes, | 468 | 476 | ) { | 469 | 477 | workInProgress.updateQueue = current.updateQueue; | 470 | | - workInProgress.flags &= ~(PassiveEffect | UpdateEffect); | | 478 | + if ( | | 479 | + __DEV__ && | | 480 | + enableDoubleInvokingEffects && | | 481 | + (workInProgress.mode & StrictMode) !== NoMode | | 482 | + ) { | | 483 | + workInProgress.flags &= ~( | | 484 | + MountLayoutDevEffect | | | 485 | + MountPassiveDevEffect | | | 486 | + PassiveEffect | | | 487 | + UpdateEffect | | 488 | + ); | | 489 | + } else { | | 490 | + workInProgress.flags &= ~(PassiveEffect | UpdateEffect); | | 491 | + } | 471 | 492 | current.lanes = removeLanes(current.lanes, lanes); | 472 | 493 | } | 473 | 494 |
| @@ -1303,12 +1324,26 @@ function mountEffect( | 1303 | 1324 | warnIfNotCurrentlyActingEffectsInDEV(currentlyRenderingFiber); | 1304 | 1325 | } | 1305 | 1326 | } | 1306 | | - return mountEffectImpl( | 1307 | | - UpdateEffect | PassiveEffect, | 1308 | | - HookPassive, | 1309 | | - create, | 1310 | | - deps, | 1311 | | - ); | | 1327 | + | | 1328 | + if ( | | 1329 | + __DEV__ && | | 1330 | + enableDoubleInvokingEffects && | | 1331 | + (currentlyRenderingFiber.mode & StrictMode) !== NoMode | | 1332 | + ) { | | 1333 | + return mountEffectImpl( | | 1334 | + UpdateEffect | PassiveEffect | MountPassiveDevEffect, | | 1335 | + HookPassive, | | 1336 | + create, | | 1337 | + deps, | | 1338 | + ); | | 1339 | + } else { | | 1340 | + return mountEffectImpl( | | 1341 | + UpdateEffect | PassiveEffect, | | 1342 | + HookPassive, | | 1343 | + create, | | 1344 | + deps, | | 1345 | + ); | | 1346 | + } | 1312 | 1347 | } | 1313 | 1348 |
| 1314 | 1349 | function updateEffect( | @@ -1333,7 +1368,20 @@ function mountLayoutEffect( | 1333 | 1368 | create: () => (() => void) | void, | 1334 | 1369 | deps: Array<mixed> | void | null, | 1335 | 1370 | ): void { | 1336 | | - return mountEffectImpl(UpdateEffect, HookLayout, create, deps); | | 1371 | + if ( | | 1372 | + __DEV__ && | | 1373 | + enableDoubleInvokingEffects && | | 1374 | + (currentlyRenderingFiber.mode & StrictMode) !== NoMode | | 1375 | + ) { | | 1376 | + return mountEffectImpl( | | 1377 | + UpdateEffect | MountLayoutDevEffect, | | 1378 | + HookLayout, | | 1379 | + create, | | 1380 | + deps, | | 1381 | + ); | | 1382 | + } else { | | 1383 | + return mountEffectImpl(UpdateEffect, HookLayout, create, deps); | | 1384 | + } | 1337 | 1385 | } | 1338 | 1386 |
| 1339 | 1387 | function updateLayoutEffect( | @@ -1392,12 +1440,25 @@ function mountImperativeHandle<T>( | 1392 | 1440 | const effectDeps = | 1393 | 1441 | deps !== null && deps !== undefined ? deps.concat([ref]) : null; | 1394 | 1442 |
| 1395 | | - return mountEffectImpl( | 1396 | | - UpdateEffect, | 1397 | | - HookLayout, | 1398 | | - imperativeHandleEffect.bind(null, create, ref), | 1399 | | - effectDeps, | 1400 | | - ); | | 1443 | + if ( | | 1444 | + __DEV__ && | | 1445 | + enableDoubleInvokingEffects && | | 1446 | + (currentlyRenderingFiber.mode & StrictMode) !== NoMode | | 1447 | + ) { | | 1448 | + return mountEffectImpl( | | 1449 | + UpdateEffect | MountLayoutDevEffect, | | 1450 | + HookLayout, | | 1451 | + imperativeHandleEffect.bind(null, create, ref), | | 1452 | + effectDeps, | | 1453 | + ); | | 1454 | + } else { | | 1455 | + return mountEffectImpl( | | 1456 | + UpdateEffect, | | 1457 | + HookLayout, | | 1458 | + imperativeHandleEffect.bind(null, create, ref), | | 1459 | + effectDeps, | | 1460 | + ); | | 1461 | + } | 1401 | 1462 | } | 1402 | 1463 |
| 1403 | 1464 | function updateImperativeHandle<T>( | @@ -1678,7 +1739,17 @@ function mountOpaqueIdentifier(): OpaqueIDType | void { | 1678 | 1739 | const setId = mountState(id)[1]; | 1679 | 1740 |
| 1680 | 1741 | if ((currentlyRenderingFiber.mode & BlockingMode) === NoMode) { | 1681 | | - currentlyRenderingFiber.flags |= UpdateEffect | PassiveEffect; | | 1742 | + if ( | | 1743 | + __DEV__ && | | 1744 | + enableDoubleInvokingEffects && | | 1745 | + (currentlyRenderingFiber.mode & StrictMode) !== NoMode | | 1746 | + ) { | | 1747 | + currentlyRenderingFiber.flags |= | | 1748 | + UpdateEffect | PassiveEffect | MountPassiveDevEffect; | | 1749 | + } else { | | 1750 | + currentlyRenderingFiber.flags |= UpdateEffect | PassiveEffect; | | 1751 | + } | | 1752 | + | 1682 | 1753 | pushEffect( | 1683 | 1754 | HookHasEffect | HookPassive, | 1684 | 1755 | () => { | |
|